-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
PutObjectAcl.go
68 lines (56 loc) · 1.97 KB
/
PutObjectAcl.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
60
61
62
63
64
65
66
67
68
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
// snippet-start:[s3.go.put_object_acl]
package main
// snippet-start:[s3.go.put_object_acl.imports]
import (
"flag"
"fmt"
"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/s3iface"
)
// snippet-end:[s3.go.put_object_acl.imports]
// PutObjectACL gives the person with EMAIL address read access to BUCKET OBJECT
func PutObjectACL(svc s3iface.S3API, bucket, key, address *string) error {
// snippet-start:[s3.go.put_object_acl.call]
_, err := svc.PutObjectAcl(&s3.PutObjectAclInput{
Bucket: bucket,
Key: key,
GrantRead: aws.String("emailaddress=" + *address),
})
// snippet-end:[s3.go.put_object_acl.call]
if err != nil {
return err
}
return nil
}
func main() {
// snippet-start:[s3.go.put_object_acl.args]
bucket := flag.String("b", "", "The bucket in which the object is stored")
key := flag.String("k", "", "The key of the object")
address := flag.String("a", "", "The email address")
flag.Parse()
if *bucket == "" || *key == "" || *address == "" {
fmt.Println("You must supply a bucket (-b BUCKET), key (-k KEY), and email address (-a ADDRESS)")
return
}
// snippet-end:[s3.go.put_object_acl.args]
// snippet-start:[s3.go.put_object_acl.session]
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
svc := s3.New(sess)
// snippet-end:[s3.go.put_object_acl.session]
err := PutObjectACL(svc, bucket, key, address)
if err != nil {
fmt.Println("Got an error retrieving the ACL for " + *key)
fmt.Println(err)
return
}
// snippet-start:[s3.go.put_object_acl.print]
fmt.Println("Congratulations. You gave user with email address", address, "read permission to bucket", bucket, "object", key)
// snippet-end:[s3.go.put_object_acl.print]
}
// snippet-end:[s3.go.put_object_acl]