s3_set_cors.go - AWS Code Sample


Sets CORS permission on an S3 bucket.

/* Copyright 2010-2020 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. */ package main 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" ) // 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() { bucketPtr := flag.String("b", "", "Bucket to set CORS on, (required)") flag.Parse() if *bucketPtr == "" { exitErrorf("-b <bucket> Bucket name required") } methods := filterMethods(flag.Args()) // Initialize a session sess := session.Must(session.NewSessionWithOptions(session.Options{ SharedConfigState: session.SharedConfigEnable, })) svc := s3.New(sess) // Create a CORS rule for the bucket 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), } // Create the parameters for the PutBucketCors API call, add add // the rule created to it. params := s3.PutBucketCorsInput{ Bucket: bucketPtr, CORSConfiguration: &s3.CORSConfiguration{ CORSRules: []*s3.CORSRule{&rule}, }, } _, err := svc.PutBucketCors(&params) 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) } func exitErrorf(msg string, args ...interface{}) { fmt.Fprintf(os.Stderr, msg+"\n", args...) os.Exit(1) } 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 }

Sample Details

Service: s3

Last tested: 2020-1-6

Author: Doug-AWS

Type: full-example