forked from awsdocs/aws-doc-sdk-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
S3CrudOps.go
196 lines (165 loc) · 4.98 KB
/
S3CrudOps.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: MIT-0
// snippet-start:[s3.go.crud_ops]
package main
// snippet-start:[s3.go.crud_ops.imports]
import (
"errors"
"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.crud_ops.imports]
// Functions to perform CRUD (create, read, update, delete) operations in Amazon S3
// MakeBucket creates an S3 bucket
func MakeBucket(svc s3iface.S3API, bucket *string) error {
// snippet-start:[s3.go.crud_ops.call]
// Create the S3 bucket
_, err := svc.CreateBucket(&s3.CreateBucketInput{
Bucket: bucket,
})
// snippet-end:[s3.go.crud_ops.call]
if err != nil {
return err
}
return nil
}
func waitForCreate(svc s3iface.S3API, bucket *string) error {
// snippet-start:[s3.go.crud_ops.wait_create]
err := svc.WaitUntilBucketExists(&s3.HeadBucketInput{
Bucket: bucket,
})
// snippet-end:[s3.go.crud_ops.wait_create]
if err != nil {
return err
}
return nil
}
func GetBucket(svc s3iface.S3API, bucket *string) error {
// snippet-start:[s3.go.crud_ops.head_bucket]
_, err := svc.HeadBucket(&s3.HeadBucketInput{
Bucket: bucket,
})
// snippet-end:[s3.go.crud_ops.head_bucket]
if err != nil {
return err
}
return nil
}
// HasACL returns nil if the S3 bucket has a read-only ACL
func HasACL(svc s3iface.S3API, bucket *string) error {
// snippet-start:[s3.go.crud_ops.get_acl]
acl, err := svc.GetBucketAcl(&s3.GetBucketAclInput{
Bucket: bucket,
})
// snippet-end:[s3.go.crud_ops.get_acl]
if err != nil {
return err
}
// Determine whether the group AllUsers has read permission
// snippet-start:[s3.go.crud_ops.check_grants]
for _, g := range acl.Grants {
if *g.Grantee.Type == "Group" && *g.Grantee.URI == "http://acs.amazonaws.com/groups/global/AllUsers" && *g.Permission == "READ" {
// snippet-end:[s3.go.crud_ops.check_grants]
return nil
}
}
return errors.New("All users do not have read access")
}
// UpdateBucket changes the S3 bucket to give all users read permission
func UpdateBucket(svc s3iface.S3API, bucket *string) error {
// snippet-start:[s3.go.crud_ops.put_acl]
_, err := svc.PutBucketAcl(&s3.PutBucketAclInput{
ACL: aws.String("public-read"),
Bucket: bucket,
})
// snippet-end:[s3.go.crud_ops.put_acl]
if err != nil {
return err
}
// Do all users have read permission?
err = HasACL(svc, bucket)
if err != nil {
return err
}
return nil
}
// RemoveBucket deletes an S3 bucket
func RemoveBucket(svc s3iface.S3API, bucket *string) error {
// snippet-start:[s3.go.crud_ops.delete_bucket]
_, err := svc.DeleteBucket(&s3.DeleteBucketInput{
Bucket: bucket,
})
// snippet-end:[s3.go.crud_ops.delete_bucket]
if err != nil {
return err
}
return nil
}
func waitForDelete(svc s3iface.S3API, bucket *string) error {
// snippet-start:[s3.go.crud_ops.wait_delete]
err := svc.WaitUntilBucketNotExists(&s3.HeadBucketInput{
Bucket: bucket,
})
// snippet-end:[s3.go.crud_ops.wait_delete]
if err != nil {
return err
}
return nil
}
func main() {
// snippet-start:[s3.go.crud_ops.args]
bucket := flag.String("b", "", "The name of the S3 bucket")
flag.Parse()
if *bucket == "" {
fmt.Println("You must supply an S3 bucket name (-b BUCKET)")
return
}
// snippet-end:[s3.go.crud_ops.args]
// snippet-start:[s3.go.crud_ops.session_service]
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
svc := s3.New(sess)
// snippet-end:[s3.go.crud_ops.session_service]
err := MakeBucket(svc, bucket)
if err != nil {
fmt.Println("Got error creating S3 bucket:")
fmt.Println(err)
return
}
err = waitForCreate(svc, bucket)
if err != nil {
fmt.Println("Got error creating S3 bucket:")
fmt.Println(err)
return
}
err = GetBucket(svc, bucket)
if err != nil {
fmt.Println("Got error reading S3 bucket:")
fmt.Println(err)
return
}
err = UpdateBucket(svc, bucket)
if err != nil {
fmt.Println("Got error updating S3 bucket:")
fmt.Println(err)
return
}
err = RemoveBucket(svc, bucket)
if err != nil {
fmt.Println("Got error deleting S3 bucket:")
fmt.Println(err)
return
}
err = waitForDelete(svc, bucket)
if err != nil {
fmt.Println("Got error deleting S3 bucket:")
fmt.Println(err)
return
}
fmt.Println("Successfully ran CRUD operations on S3 bucket " + *bucket)
}
// snippet-end:[s3.go.crud_ops]