AWS SDK for Go S3バケット基本操作


AWS SDK for Go

公式Doc

https://docs.aws.amazon.com/ja_jp/sdk-for-go/v1/developer-guide/s3-example-basic-bucket-operations.html


S3と接続するインスタンスの生成

import (

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/service/s3"
)

creds := credentials.NewStaticCredentials("AWS_ACCESS_KEY", "AWS_SECRET_ACCESS_KEY", "")
sess, err := session.NewSession(&aws.Config{
Credentials: creds,
Region: aws.String("ap-northeast-1")},
)
svc := s3.New(sess)


バケットの一覧を取得:ListBuckets

result, _ := svc.ListBuckets(nil)

fmt.Println("Buckets:")

for _, b := range result.Buckets {
fmt.Printf("* %s created on %s\n", aws.StringValue(b.Name), aws.TimeValue(b.CreationDate))
}


バケットの作成:CreateBucket

bucket := "new-bucket-name"

resp, err := svc.CreateBucket(&s3.CreateBucketInput{
Bucket: aws.String(bucket),
})
fmt.Println(resp) // -> { Location: "http://new-bucket-name.s3.amazonaws.com/" }


バケット内のオブジェクト一覧を取得:ListObjects

bucket := "bucket-name"

resp, err := svc.ListObjects(&s3.ListObjectsInput{Bucket: aws.String(bucket)})

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("")
}


ファイルのアップロード:Upload

import "github.com/aws/aws-sdk-go/service/s3/s3manager"

bucket := "bucket-name"
filename := "filename"
file, err := os.Open(filename)

defer file.Close()

uploader := s3manager.NewUploader(sess)
_, err = uploader.Upload(&s3manager.UploadInput{
Bucket: aws.String(bucket),
Key: aws.String(filename),
Body: file,
})


ファイルのダウンロード:Download

import "github.com/aws/aws-sdk-go/service/s3/s3manager"

bucket := "bucket-name"
file, err := os.Create("filename")

defer file.Close()

downloader := s3manager.NewDownloader(sess)
numBytes, err := downloader.Download(file,
&s3.GetObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(item),
})


バケット間でファイルをコピー:CopyObject

bucket := "bucket-containing-file"

item := "file-to-copy"
other := "bucket-to-file-copied"

_, err = svc.CopyObject(&s3.CopyObjectInput{Bucket: aws.String(other), CopySource: aws.String(source), Key: aws.String(item)})


ファイルの削除:DeleteObject

bucket := "bucket-name"

obj := "file-to-delete"

_, err = svc.DeleteObject(&s3.DeleteObjectInput{Bucket: aws.String(bucket), Key: aws.String(obj)})

err = svc.WaitUntilObjectNotExists(&s3.HeadObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(obj),
}) // errorが帰ってこなければ成功


全てのファイルを削除:DeleteObjects

import "github.com/aws/aws-sdk-go/service/s3/s3manager"

bucket := "bucket-name"

iter := s3manager.NewDeleteListIterator(svc, &s3.ListObjectsInput{
Bucket: aws.String(bucket),
})

if err := s3manager.NewBatchDeleteWithClient(svc).Delete(aws.BackgroundContext(), iter); err != nil {
fmt.Println(err)
}


アーカイブされたファイルの復元:RestoreObject

obj := "file-to-restore"

_, err = svc.RestoreObject(&s3.RestoreObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(obj),
RestoreRequest: &s3.RestoreRequest{Days: aws.Int64(30)}
})


バケットの削除:DeleteBucket

bucket := "bucket-name"

_, err = svc.DeleteBucket(&s3.DeleteBucketInput{
Bucket: aws.String(bucket),
})

err = svc.WaitUntilBucketNotExists(&s3.HeadBucketInput{
Bucket: aws.String(bucket),
})