Menu
AWS SDK for Go
Developer Guide

Working with Amazon S3 CORS Permissions

This AWS SDK for Go example shows you how to list Amazon S3 buckets and configure CORS and bucket logging. You can download complete versions of these example files from the aws-doc-sdk-examples repository on GitHub.

Scenario

In this example, a series of Go routines are used to list your Amazon S3 buckets and to configure CORS and bucket logging. The routines use the AWS SDK for Go to configure a selected Amazon S3 bucket using these methods of the Amazon S3 client class:

If you are unfamiliar with using CORS configuration with an Amazon S3 bucket, it's worth your time to read Cross-Origin Resource Sharing (CORS) in the Amazon S3 Developer Guide before proceeding.

Prerequisites

Configure CORS on the Bucket

Create a new Go file named s3_set_cors.go. You must import the relevant Go and AWS SDK for Go packages by adding the following lines.

Copy
import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/s3" "flag" "fmt" "os" "strings" )

This routine configures CORS rules for a bucket by setting the allowed HTTP methods.

It requires the bucket name and can also take a space-separated list of HTTP methods. Using the Go Language's flag package, it parses the input and validates the bucket name.

Copy
var bucket string // Setup the CLI options and validation flag.StringVar(&bucket, "b", "", "Bucket to set CORS on, (required)") flag.Parse() if len(bucket) == 0 { exitErrorf("-b <bucket> Bucket name required") } methods := filterMethods(flag.Args())

Notice the helper method, filterMethods, which ensures the methods passed in are uppercase.

Copy
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 }

Initialize a session that the SDK will use to load credentials, from the shared credentials file, ~/.aws/credentials, and create a new S3 service client.

Copy
sess, err := session.NewSession(&aws.Config{ Region: aws.String("us-west-2")}, ) // Create S3 service client svc := s3.New(sess)

Create a new CORSRule to set up the CORS configuration.

Copy
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), }

Add the CORSRule to the PutBucketCorsInput structure, call PutBucketCors with that structure, and print a success or error message.

Copy
params := s3.PutBucketCorsInput{ Bucket: aws.String(bucket), 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", bucket, err) } // Print the updated CORS config for the bucket fmt.Printf("Updated bucket %q CORS for %v\n", bucket, methods)