-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
s3_set_cors.go
107 lines (88 loc) · 2.7 KB
/
s3_set_cors.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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
// snippet-start:[s3.go.set_cors.complete]
package main
// snippet-start:[s3.go.set_cors.imports]
import (
"flag"
"fmt"
"os"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
)
// snippet-end:[s3.go.set_cors.imports]
// Configures CORS rules for a bucket by setting the allowed
// HTTP methods.
//
// Requires the bucket name, and can also take a space separated
// list of HTTP methods.
//
// Usage:
//
// go run s3_set_cors.go -b BUCKET_NAME get put
func main() {
// snippet-start:[s3.go.set_cors.vars]
bucketPtr := flag.String("b", "", "Bucket to set CORS on, (required)")
flag.Parse()
if *bucketPtr == "" {
exitErrorf("-b <bucket> Bucket name required")
}
methods := filterMethods(flag.Args())
// snippet-end:[s3.go.set_cors.vars]
// Initialize a session
// snippet-start:[s3.go.set_cors.session]
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
svc := s3.New(sess)
// snippet-end:[s3.go.set_cors.session]
// Create a CORS rule for the bucket
// snippet-start:[s3.go.set_cors.rule]
rule := s3.CORSRule{
AllowedHeaders: aws.StringSlice([]string{"Authorization"}),
AllowedOrigins: aws.StringSlice([]string{"*"}),
MaxAgeSeconds: aws.Int64(3000),
// Add HTTP methods CORS request that were specified in the CLI.
AllowedMethods: aws.StringSlice(methods),
}
// snippet-end:[s3.go.set_cors.rule]
// Create the parameters for the PutBucketCors API call, add add
// the rule created to it.
// snippet-start:[s3.go.set_cors.put]
params := s3.PutBucketCorsInput{
Bucket: bucketPtr,
CORSConfiguration: &s3.CORSConfiguration{
CORSRules: []*s3.CORSRule{&rule},
},
}
_, err := svc.PutBucketCors(¶ms)
if err != nil {
// Print the error message
exitErrorf("Unable to set Bucket %q's CORS, %v", *bucketPtr, err)
}
// Print the updated CORS config for the bucket
fmt.Printf("Updated bucket %q CORS for %v\n", *bucketPtr, methods)
// snippet-end:[s3.go.set_cors.put]
}
// snippet-start:[s3.go.set_cors.exit]
func exitErrorf(msg string, args ...interface{}) {
fmt.Fprintf(os.Stderr, msg+"\n", args...)
os.Exit(1)
}
// snippet-end:[s3.go.set_cors.exit]
// snippet-start:[s3.go.set_cors.filter]
func filterMethods(methods []string) []string {
filtered := make([]string, 0, len(methods))
for _, m := range methods {
v := strings.ToUpper(m)
switch v {
case "POST", "GET", "PUT", "PATCH", "DELETE":
filtered = append(filtered, v)
}
}
return filtered
}
// snippet-end:[s3.go.set_cors.filter]
// snippet-end:[s3.go.set_cors.complete]