2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

golangで、S3のBucket以下のObjectを全て操作する

Last updated at Posted at 2016-10-15

いくつかのサイトで、S3のObjectの操作を調べたのですが、有名なサイトでも、私が望んていた処理が一部満たされていない(1,000件以上のObjectの操作が出来ないなど)かったので、今回 go-lang で実装してみました。

注意点

  • AWS の API の仕様で、一度に1,000件までの Object しか取得できない
  • ec2 上で実行した方が、料金を抑えることができるはず

前提条件

  • AWS CLI の設定が終わっていること。

実装


package main

import (
	"flag"
	"fmt"

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

var bucket string

// Regsion
const Region = "us-west-1"

func main() {
	flag.Parse()
	fmt.Println("flag.Args: ", flag.Args())
	fmt.Println(flag.Args()[0])

	bucket = flag.Args()[0]

	getS3Objects("prefix/")

}

func getS3Objects(prefix string) bool {

	var re = Region
	cli := s3.New(&aws.Config{Region: &re})

	params := &s3.ListObjectsInput{
		Bucket: aws.String(bucket),
		Prefix: aws.String(prefix),
	}

	cli.ListObjectsPages(params, addAclPublicRead)

	return true
}

func addAclPublicRead(resps *s3.ListObjectsOutput, more bool) bool {

	var re = Region
	cli := s3.New(&aws.Config{Region: &re})

	for n := 0; n < len(resps.Contents); n++ {
		params := &s3.PutObjectAclInput{
			Bucket: aws.String(bucket),
			Key:    aws.String(*resps.Contents[n].Key),
			ACL:    aws.String(s3.ObjectCannedACLPublicRead),
		}
		resp, err := cli.PutObjectAcl(params)

		if err != nil {
			fmt.Println(err.Error())
			return false
		}

		// Pretty-print the response data.
		fmt.Println(resp)
	}
	return true
}
2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?