iwinv Object Storage – Go SDK

| 2020년 9월 1일 | 0 Comments

 

 

 

해당 메뉴얼은 AWS S3에서 제공하는 Go SDK를 이용하여 iwinv Object Storage를 사용하는 방법을 설명합니다.

 

참고문서 

https://docs.aws.amazon.com/ko_kr/sdk-for-go/v1/developer-guide

https://github.com/awsdocs/aws-doc-sdk-examples/tree/master/go 

 

Go SDK 사용하기


테스트 OS : Ubuntu 18.04 LTS

 

[인증키 확인]

Endpoint -> 오브젝트 스토리지 -> 스토리지 관리 -> 인증키 관리 -> API {IDC} Endpoint    

Access_Key  : console.iwinv.kr -> 오브젝트 스토리지 -> 스토리지 관리 -> 인증키 관리 -> Access Key ID

Secret_Key   : console.iwinv.kr -> 오브젝트 스토리지 -> 스토리지 관리 -> 인증키 관리 -> Secret Key ID

 

 

 

Go 설치하기

apt install go-lang

 

Go SDK 설치하기  

go get -u github.com/aws/aws-sdk-go

 

 

# 예제코드  


 

버킷 생성

package main

import (
“github.com/aws/aws-sdk-go/aws”
“github.com/aws/aws-sdk-go/aws/session”
“github.com/aws/aws-sdk-go/service/s3”
“github.com/aws/aws-sdk-go/aws/credentials”
“fmt”
“os”
)

func main() {
if len(os.Args) != 2 {
exitErrorf(“Bucket name missing!\nUsage: %s bucket_name”, os.Args[0])
}
bucket := os.Args[1]

sess, err := session.NewSession(&aws.Config{
Endpoint: aws.String(“Endpoint“),
Region: aws.String(“default”),
Credentials: credentials.NewStaticCredentials(“Access_Key“, “Secret_Key“, “”),
})

svc := s3.New(sess)

_, err = svc.CreateBucket(&s3.CreateBucketInput{
Bucket: aws.String(bucket),
})
if err != nil {
exitErrorf(“Unable to create bucket %q, %v”, bucket, err)
}

fmt.Printf(“Waiting for bucket %q to be created…\n”, bucket)

err = svc.WaitUntilBucketExists(&s3.HeadBucketInput{
Bucket: aws.String(bucket),
})
if err != nil {
exitErrorf(“Error occurred while waiting for bucket to be created, %v”, bucket)
}

fmt.Printf(“Bucket %q successfully created\n”, bucket)
}

func exitErrorf(msg string, args …interface{}) {
fmt.Fprintf(os.Stderr, msg+”\n”, args…)
os.Exit(1)
}

 

# 실행 명령어 

go run 파일명.go [Bucket_Name]

 

 

버킷 삭제

package main

import (
“github.com/aws/aws-sdk-go/aws”
“github.com/aws/aws-sdk-go/aws/session”
“github.com/aws/aws-sdk-go/service/s3”
“github.com/aws/aws-sdk-go/aws/credentials”
“fmt”
“os”
)

func main() {
if len(os.Args) != 2 {
exitErrorf(“bucket name required\nUsage: %s bucket_name”, os.Args[0])
}

bucket := os.Args[1]

sess, err := session.NewSession(&aws.Config{
Endpoint: aws.String(“Endpoint“),
Region: aws.String(“default”),
Credentials: credentials.NewStaticCredentials(“Access_Key“, “Secret_Key“, “”),
})

svc := s3.New(sess)

_, err = svc.DeleteBucket(&s3.DeleteBucketInput{
Bucket: aws.String(bucket),
})
if err != nil {
exitErrorf(“Unable to delete bucket %q, %v”, bucket, err)
}

fmt.Printf(“Waiting for bucket %q to be deleted…\n”, bucket)

err = svc.WaitUntilBucketNotExists(&s3.HeadBucketInput{
Bucket: aws.String(bucket),
})
if err != nil {
exitErrorf(“Error occurred while waiting for bucket to be deleted, %v”, bucket)
}

fmt.Printf(“Bucket %q successfully deleted\n”, bucket)
}

func exitErrorf(msg string, args …interface{}) {
fmt.Fprintf(os.Stderr, msg+”\n”, args…)
os.Exit(1)
}

 

# 실행 명령어 

go run 파일명.go [Bucket_Name]

 

 

버킷 리스트 확인

package main

import (
“github.com/aws/aws-sdk-go/aws”
“github.com/aws/aws-sdk-go/aws/session”
“github.com/aws/aws-sdk-go/service/s3”
“github.com/aws/aws-sdk-go/aws/credentials”
“log”
)

func main() {
sess, err := session.NewSession(&aws.Config{
Endpoint: aws.String(“Endpoint“),
Region: aws.String(“default”),
Credentials: credentials.NewStaticCredentials(“Access_Key“, “Secret_Key“, “”),
})

svc := s3.New(sess)

result, err := svc.ListBuckets(&s3.ListBucketsInput{})
if err != nil {
log.Println(“Failed to list buckets”, err)
return
}

log.Println(“Buckets:”)

for _, bucket := range result.Buckets {
log.Printf(“%s : %s\n”, aws.StringValue(bucket.Name), bucket.CreationDate)
}
}

 

 

오브젝트 업로드

package main

import (
“github.com/aws/aws-sdk-go/aws”
“github.com/aws/aws-sdk-go/aws/session”
“github.com/aws/aws-sdk-go/service/s3/s3manager”
“github.com/aws/aws-sdk-go/aws/credentials”
“fmt”
“os”
)

func main() {
if len(os.Args) != 3 {
exitErrorf(“bucket and file name required\nUsage: %s bucket_name filename”,
os.Args[0])
}

bucket := os.Args[1]
filename := os.Args[2]

file, err := os.Open(filename)
if err != nil {
exitErrorf(“Unable to open file %q, %v”, err)
}

defer file.Close()

sess, err := session.NewSession(&aws.Config{
Endpoint: aws.String(“Endpoint“),
Region: aws.String(“default”),
Credentials: credentials.NewStaticCredentials(“Access_Key“, “Secret_Key“, “”),
})
uploader := s3manager.NewUploader(sess)

_, err = uploader.Upload(&s3manager.UploadInput{
Bucket: aws.String(bucket),

Key: aws.String(filename),

Body: file,
})
if err != nil {
// Print the error and exit.
exitErrorf(“Unable to upload %q to %q, %v”, filename, bucket, err)
}

fmt.Printf(“Successfully uploaded %q to %q\n”, filename, bucket)
}

func exitErrorf(msg string, args …interface{}) {
fmt.Fprintf(os.Stderr, msg+”\n”, args…)
os.Exit(1)
}

 

# 실행 명령어 

go run 파일명.go [Bucket_Name] [Path/File_Name] 

ex) go run object_upload.go iwinv-bucket /tmp/test.txt   

 

* 업로드시 디렉토리도 함께 버킷에 업로드됩니다. 

(해당 디렉토리에서 명령어 실행시에는 파일명만 입력 – 파일만 버킷에 업로드)

 

 

오브젝트 다운로드

package main

import (
“github.com/aws/aws-sdk-go/aws”
“github.com/aws/aws-sdk-go/aws/session”
“github.com/aws/aws-sdk-go/service/s3”
“github.com/aws/aws-sdk-go/service/s3/s3manager”
“github.com/aws/aws-sdk-go/aws/credentials”
“fmt”
“os”
)

func main() {
if len(os.Args) != 3 {
exitErrorf(“Bucket and item names required\nUsage: %s bucket_name item_name”,
os.Args[0])
}

bucket := os.Args[1]
item := os.Args[2]

file, err := os.Create(item)
if err != nil {
exitErrorf(“Unable to open file %q, %v”, item, err)
}

defer file.Close()

sess, err := session.NewSession(&aws.Config{
Endpoint: aws.String(“Endpoint“),
Region: aws.String(“default”),
Credentials: credentials.NewStaticCredentials(“Access_Key“, “Secret_Key“, “”),
})

downloader := s3manager.NewDownloader(sess)

numBytes, err := downloader.Download(file,
&s3.GetObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(item),
})
if err != nil {
exitErrorf(“Unable to download item %q, %v”, item, err)
}

fmt.Println(“Downloaded”, file.Name(), numBytes, “bytes”)
}

func exitErrorf(msg string, args …interface{}) {
fmt.Fprintf(os.Stderr, msg+”\n”, args…)
os.Exit(1)
}

 

# 실행 명령어 

go run 파일명.go [Bucket_Name] [Object_Name]

 

* 다운로드시 명령어를 실행한 디렉토리에 파일이 저장됩니다.

 

 

오브젝트 리스트 확인

package main

import (
“github.com/aws/aws-sdk-go/aws”
“github.com/aws/aws-sdk-go/aws/session”
“github.com/aws/aws-sdk-go/service/s3”
“github.com/aws/aws-sdk-go/aws/credentials”
“fmt”
“os”
)

func main() {
if len(os.Args) != 2 {
exitErrorf(“Bucket name required\nUsage: %s bucket_name”,
os.Args[0])
}

bucket := os.Args[1]

sess, err := session.NewSession(&aws.Config{
Endpoint: aws.String(“Endpoint“),
Region: aws.String(“default”),
Credentials: credentials.NewStaticCredentials(“Access_Key“, “Secret_Key“, “”),
})

svc := s3.New(sess)

resp, err := svc.ListObjectsV2(&s3.ListObjectsV2Input{Bucket: aws.String(bucket)})
if err != nil {
exitErrorf(“Unable to list items in bucket %q, %v”, bucket, err)
}

for _, item := range resp.Contents {
fmt.Println(“Name: “, *item.Key)
fmt.Println(“Last modified:”, *item.LastModified)
fmt.Println(“Size: “, *item.Size)
fmt.Println(“Storage class:”, *item.StorageClass)
fmt.Println(“”)
}

fmt.Println(“Found”, len(resp.Contents), “items in bucket”, bucket)
fmt.Println(“”)
}

func exitErrorf(msg string, args …interface{}) {
fmt.Fprintf(os.Stderr, msg+”\n”, args…)
os.Exit(1)
}

 

 

오브젝트 삭제

package main

import (
“github.com/aws/aws-sdk-go/aws”
“github.com/aws/aws-sdk-go/aws/session”
“github.com/aws/aws-sdk-go/service/s3”
“github.com/aws/aws-sdk-go/aws/credentials”
“fmt”
“os”
)

func main() {
if len(os.Args) != 3 {
exitErrorf(“Bucket and object name required\nUsage: %s bucket_name object_name”,
os.Args[0])
}

bucket := os.Args[1]
obj := os.Args[2]

sess, err := session.NewSession(&aws.Config{
Endpoint: aws.String(“Endpoint“),
Region: aws.String(“default”),
Credentials: credentials.NewStaticCredentials(“Access_Key“, “Secret_Key“, “”),
})

svc := s3.New(sess)

_, err = svc.DeleteObject(&s3.DeleteObjectInput{Bucket: aws.String(bucket), Key: aws.String(obj)})
if err != nil {
exitErrorf(“Unable to delete object %q from bucket %q, %v”, obj, bucket, err)
}

err = svc.WaitUntilObjectNotExists(&s3.HeadObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(obj),
})
if err != nil {
exitErrorf(“Error occurred while waiting for object %q to be deleted, %v”, obj, err)
}

fmt.Printf(“Object %q successfully deleted\n”, obj)
}

func exitErrorf(msg string, args …interface{}) {
fmt.Fprintf(os.Stderr, msg+”\n”, args…)
os.Exit(1)
}

 

# 실행 명령어 

go run 파일명.go [Bucket_Name] [Object_Name] 

 

Category: 가상화/클라우드

Avatar

About the Author ()