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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
|
// +build example
package main
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"
)
// Put an ACL on an S3 object
//
// Usage:
// putBucketAcl <params>
// -region <region> // required
// -bucket <bucket> // required
// -key <key> // required
// -owner-name <owner-name>
// -owner-id <owner-id>
// -grantee-type <some type> // required
// -uri <uri to group>
// -email <email address>
// -user-id <user-id>
func main() {
regionPtr := flag.String("region", "", "region of your request")
bucketPtr := flag.String("bucket", "", "name of your bucket")
keyPtr := flag.String("key", "", "of your object")
ownerNamePtr := flag.String("owner-name", "", "of your request")
ownerIDPtr := flag.String("owner-id", "", "of your request")
granteeTypePtr := flag.String("grantee-type", "", "of your request")
uriPtr := flag.String("uri", "", "of your grantee type")
emailPtr := flag.String("email", "", "of your grantee type")
userPtr := flag.String("user-id", "", "of your grantee type")
displayNamePtr := flag.String("display-name", "", "of your grantee type")
flag.Parse()
// Based off the type, fields must be excluded.
switch *granteeTypePtr {
case s3.TypeCanonicalUser:
emailPtr, uriPtr = nil, nil
if *displayNamePtr == "" {
displayNamePtr = nil
}
if *userPtr == "" {
userPtr = nil
}
case s3.TypeAmazonCustomerByEmail:
uriPtr, userPtr = nil, nil
case s3.TypeGroup:
emailPtr, userPtr = nil, nil
}
sess := session.Must(session.NewSession(&aws.Config{
Region: regionPtr,
}))
svc := s3.New(sess)
resp, err := svc.PutObjectAcl(&s3.PutObjectAclInput{
Bucket: bucketPtr,
Key: keyPtr,
AccessControlPolicy: &s3.AccessControlPolicy{
Owner: &s3.Owner{
DisplayName: ownerNamePtr,
ID: ownerIDPtr,
},
Grants: []*s3.Grant{
{
Grantee: &s3.Grantee{
Type: granteeTypePtr,
DisplayName: displayNamePtr,
URI: uriPtr,
EmailAddress: emailPtr,
ID: userPtr,
},
Permission: aws.String(s3.BucketLogsPermissionFullControl),
},
},
},
})
if err != nil {
fmt.Println("failed", err)
} else {
fmt.Println("success", resp)
}
}
|