-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateS3Bucket.go
46 lines (36 loc) · 1.2 KB
/
createS3Bucket.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
package main
import (
"context"
"fmt"
"log"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/aws/aws-sdk-go-v2/service/s3/types"
)
func main() {
// AWS Region & Bucket Vars
region := "us-west-2"
bucketName := "faddah-go-test-bucket"
// Create a new service client — VPC, S3, Lambda, DynamoDB client, etc.
cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithRegion(region))
if err != nil {
log.Fatalf("unable to load AWS SDK User config, %v", err)
}
log.Printf("AWS SDK config loaded: %v", cfg)
// Create a new service client — VPC, S3, Lambda, DynamoDB client, etc.
svc := s3.NewFromConfig(cfg)
// Create input variables & location constratint
s3Input := &s3.CreateBucketInput{
Bucket: aws.String(bucketName),
CreateBucketConfiguration: &types.CreateBucketConfiguration{
LocationConstraint: types.BucketLocationConstraint(region),
},
}
// Call the actual function to do stuff
_, err = svc.CreateBucket(context.TODO(), s3Input)
if err != nil {
log.Fatalf("Failed to create the AWS S3 Bucket: %v.", err)
}
fmt.Println("AWS S3 Bucket %s Created in Region %s.\n", bucketName, region)
}