Help us understand the problem. What is going on with this article?

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

More than 3 years have passed since last update.

いくつかのサイトで、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
}
urapico
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした