forked from awsdocs/aws-doc-sdk-examples
/
s3.go
116 lines (93 loc) · 3.08 KB
/
s3.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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
/**
* Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* This file is licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License. A copy of
* the License is located at
*
* http://aws.amazon.com/apache2.0/
*
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
// snippet-start:[s3.go.bucket_operations.list_create_delete]
package main
import (
"fmt"
"os"
"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() {
if len(os.Args) < 3 {
fmt.Printf("Usage: go run s3.go <the bucket name> <the AWS Region to use>\n" +
"Example: go run s3.go my-test-bucket us-east-2\n")
os.Exit(1)
}
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
svc := s3.New(sess, &aws.Config{
Region: aws.String(os.Args[2]),
})
listMyBuckets(svc)
createMyBucket(svc, os.Args[1], os.Args[2])
listMyBuckets(svc)
deleteMyBucket(svc, os.Args[1])
listMyBuckets(svc)
}
// List all of your available buckets in this AWS Region.
func listMyBuckets(svc *s3.S3) {
result, err := svc.ListBuckets(nil)
if err != nil {
exitErrorf("Unable to list buckets, %v", err)
}
fmt.Println("My buckets now are:\n")
for _, b := range result.Buckets {
fmt.Printf(aws.StringValue(b.Name) + "\n")
}
fmt.Printf("\n")
}
// Create a bucket in this AWS Region.
func createMyBucket(svc *s3.S3, bucketName string, region string) {
fmt.Printf("\nCreating a new bucket named '" + bucketName + "'...\n\n")
_, err := svc.CreateBucket(&s3.CreateBucketInput{
Bucket: aws.String(bucketName),
CreateBucketConfiguration: &s3.CreateBucketConfiguration{
LocationConstraint: aws.String(region),
},
})
if err != nil {
exitErrorf("Unable to create bucket, %v", err)
}
// Wait until bucket is created before finishing
fmt.Printf("Waiting for bucket %q to be created...\n", bucketName)
err = svc.WaitUntilBucketExists(&s3.HeadBucketInput{
Bucket: aws.String(bucketName),
})
}
// Delete the bucket you just created.
func deleteMyBucket(svc *s3.S3, bucketName string) {
fmt.Printf("\nDeleting the bucket named '" + bucketName + "'...\n\n")
_, err := svc.DeleteBucket(&s3.DeleteBucketInput{
Bucket: aws.String(bucketName),
})
if err != nil {
exitErrorf("Unable to delete bucket, %v", err)
}
// Wait until bucket is deleted before finishing
fmt.Printf("Waiting for bucket %q to be deleted...\n", bucketName)
err = svc.WaitUntilBucketNotExists(&s3.HeadBucketInput{
Bucket: aws.String(bucketName),
})
}
// If there's an error, display it.
func exitErrorf(msg string, args ...interface{}) {
fmt.Fprintf(os.Stderr, msg+"\n", args...)
os.Exit(1)
}
// snippet-end:[s3.go.bucket_operations.list_create_delete]