Path: blob/master/code_examples/java_examples/S3Examples/CORS.java
4084 views
// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.1// SPDX-License-Identifier: MIT-0 (For details, see https://github.com/awsdocs/amazon-s3-developer-guide/blob/master/LICENSE-SAMPLECODE.)23import java.io.IOException;4import java.util.ArrayList;5import java.util.Arrays;6import java.util.List;78import com.amazonaws.AmazonServiceException;9import com.amazonaws.SdkClientException;10import com.amazonaws.auth.profile.ProfileCredentialsProvider;11import com.amazonaws.services.s3.AmazonS3;12import com.amazonaws.services.s3.AmazonS3ClientBuilder;13import com.amazonaws.services.s3.model.BucketCrossOriginConfiguration;14import com.amazonaws.services.s3.model.CORSRule;1516public class CORS {1718public static void main(String[] args) throws IOException {19String clientRegion = "*** Client region ***";20String bucketName = "*** Bucket name ***";2122// Create two CORS rules.23List<CORSRule.AllowedMethods> rule1AM = new ArrayList<CORSRule.AllowedMethods>();24rule1AM.add(CORSRule.AllowedMethods.PUT);25rule1AM.add(CORSRule.AllowedMethods.POST);26rule1AM.add(CORSRule.AllowedMethods.DELETE);27CORSRule rule1 = new CORSRule().withId("CORSRule1").withAllowedMethods(rule1AM)28.withAllowedOrigins(Arrays.asList(new String[] { "http://*.example.com" }));2930List<CORSRule.AllowedMethods> rule2AM = new ArrayList<CORSRule.AllowedMethods>();31rule2AM.add(CORSRule.AllowedMethods.GET);32CORSRule rule2 = new CORSRule().withId("CORSRule2").withAllowedMethods(rule2AM)33.withAllowedOrigins(Arrays.asList(new String[] { "*" })).withMaxAgeSeconds(3000)34.withExposedHeaders(Arrays.asList(new String[] { "x-amz-server-side-encryption" }));3536List<CORSRule> rules = new ArrayList<CORSRule>();37rules.add(rule1);38rules.add(rule2);3940// Add the rules to a new CORS configuration.41BucketCrossOriginConfiguration configuration = new BucketCrossOriginConfiguration();42configuration.setRules(rules);4344try {45AmazonS3 s3Client = AmazonS3ClientBuilder.standard()46.withCredentials(new ProfileCredentialsProvider())47.withRegion(clientRegion)48.build();4950// Add the configuration to the bucket.51s3Client.setBucketCrossOriginConfiguration(bucketName, configuration);5253// Retrieve and display the configuration.54configuration = s3Client.getBucketCrossOriginConfiguration(bucketName);55printCORSConfiguration(configuration);5657// Add another new rule.58List<CORSRule.AllowedMethods> rule3AM = new ArrayList<CORSRule.AllowedMethods>();59rule3AM.add(CORSRule.AllowedMethods.HEAD);60CORSRule rule3 = new CORSRule().withId("CORSRule3").withAllowedMethods(rule3AM)61.withAllowedOrigins(Arrays.asList(new String[] { "http://www.example.com" }));6263rules = configuration.getRules();64rules.add(rule3);65configuration.setRules(rules);66s3Client.setBucketCrossOriginConfiguration(bucketName, configuration);6768// Verify that the new rule was added by checking the number of rules in the configuration.69configuration = s3Client.getBucketCrossOriginConfiguration(bucketName);70System.out.println("Expected # of rules = 3, found " + configuration.getRules().size());7172// Delete the configuration.73s3Client.deleteBucketCrossOriginConfiguration(bucketName);74System.out.println("Removed CORS configuration.");7576// Retrieve and display the configuration to verify that it was77// successfully deleted.78configuration = s3Client.getBucketCrossOriginConfiguration(bucketName);79printCORSConfiguration(configuration);80}81catch(AmazonServiceException e) {82// The call was transmitted successfully, but Amazon S3 couldn't process83// it, so it returned an error response.84e.printStackTrace();85}86catch(SdkClientException e) {87// Amazon S3 couldn't be contacted for a response, or the client88// couldn't parse the response from Amazon S3.89e.printStackTrace();90}91}9293private static void printCORSConfiguration(BucketCrossOriginConfiguration configuration) {94if (configuration == null) {95System.out.println("Configuration is null.");96} else {97System.out.println("Configuration has " + configuration.getRules().size() + " rules\n");9899for (CORSRule rule : configuration.getRules()) {100System.out.println("Rule ID: " + rule.getId());101System.out.println("MaxAgeSeconds: " + rule.getMaxAgeSeconds());102System.out.println("AllowedMethod: " + rule.getAllowedMethods());103System.out.println("AllowedOrigins: " + rule.getAllowedOrigins());104System.out.println("AllowedHeaders: " + rule.getAllowedHeaders());105System.out.println("ExposeHeader: " + rule.getExposedHeaders());106System.out.println();107}108}109}110}111112