-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy paths3-recursive-acl.go
59 lines (51 loc) · 1.51 KB
/
s3-recursive-acl.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package main
import (
"flag"
"fmt"
"os"
"sync"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
)
func main() {
var bucket, region, path, cannedACL string
var wg sync.WaitGroup
var counter int64
flag.StringVar(®ion, "region", "ap-northeast-1", "AWS region")
flag.StringVar(&bucket, "bucket", "s3-bucket", "Bucket name")
flag.StringVar(&path, "path", "/", "Path to recurse under")
flag.StringVar(&cannedACL, "acl", "public-read", "Canned ACL to assign objects")
flag.Parse()
svc := s3.New(session.New(), &aws.Config{
Region: aws.String(region),
})
err := svc.ListObjectsPages(&s3.ListObjectsInput{
Prefix: aws.String(path),
Bucket: aws.String(bucket),
}, func(page *s3.ListObjectsOutput, lastPage bool) bool {
for _, object := range page.Contents {
key := *object.Key
counter++
go func(bucket string, key string, cannedACL string) {
wg.Add(1)
_, err := svc.PutObjectAcl(&s3.PutObjectAclInput{
ACL: aws.String(cannedACL),
Bucket: aws.String(bucket),
Key: aws.String(key),
})
fmt.Println(fmt.Sprintf("Updating '%s'", key))
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to change permissions on '%s', %v", key, err)
}
defer wg.Done()
}(bucket, key, cannedACL)
}
return true
})
wg.Wait()
if err != nil {
panic(fmt.Sprintf("Failed to update object permissions in '%s', %v", bucket, err))
}
fmt.Println(fmt.Sprintf("Successfully updated permissions on %d objects", counter))
}