Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
awsdocs
GitHub Repository: awsdocs/amazon-s3-developer-guide
Path: blob/master/code_examples/java_examples/S3Examples/CORS.java
4084 views
1
// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
// SPDX-License-Identifier: MIT-0 (For details, see https://github.com/awsdocs/amazon-s3-developer-guide/blob/master/LICENSE-SAMPLECODE.)
3
4
import java.io.IOException;
5
import java.util.ArrayList;
6
import java.util.Arrays;
7
import java.util.List;
8
9
import com.amazonaws.AmazonServiceException;
10
import com.amazonaws.SdkClientException;
11
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
12
import com.amazonaws.services.s3.AmazonS3;
13
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
14
import com.amazonaws.services.s3.model.BucketCrossOriginConfiguration;
15
import com.amazonaws.services.s3.model.CORSRule;
16
17
public class CORS {
18
19
public static void main(String[] args) throws IOException {
20
String clientRegion = "*** Client region ***";
21
String bucketName = "*** Bucket name ***";
22
23
// Create two CORS rules.
24
List<CORSRule.AllowedMethods> rule1AM = new ArrayList<CORSRule.AllowedMethods>();
25
rule1AM.add(CORSRule.AllowedMethods.PUT);
26
rule1AM.add(CORSRule.AllowedMethods.POST);
27
rule1AM.add(CORSRule.AllowedMethods.DELETE);
28
CORSRule rule1 = new CORSRule().withId("CORSRule1").withAllowedMethods(rule1AM)
29
.withAllowedOrigins(Arrays.asList(new String[] { "http://*.example.com" }));
30
31
List<CORSRule.AllowedMethods> rule2AM = new ArrayList<CORSRule.AllowedMethods>();
32
rule2AM.add(CORSRule.AllowedMethods.GET);
33
CORSRule rule2 = new CORSRule().withId("CORSRule2").withAllowedMethods(rule2AM)
34
.withAllowedOrigins(Arrays.asList(new String[] { "*" })).withMaxAgeSeconds(3000)
35
.withExposedHeaders(Arrays.asList(new String[] { "x-amz-server-side-encryption" }));
36
37
List<CORSRule> rules = new ArrayList<CORSRule>();
38
rules.add(rule1);
39
rules.add(rule2);
40
41
// Add the rules to a new CORS configuration.
42
BucketCrossOriginConfiguration configuration = new BucketCrossOriginConfiguration();
43
configuration.setRules(rules);
44
45
try {
46
AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
47
.withCredentials(new ProfileCredentialsProvider())
48
.withRegion(clientRegion)
49
.build();
50
51
// Add the configuration to the bucket.
52
s3Client.setBucketCrossOriginConfiguration(bucketName, configuration);
53
54
// Retrieve and display the configuration.
55
configuration = s3Client.getBucketCrossOriginConfiguration(bucketName);
56
printCORSConfiguration(configuration);
57
58
// Add another new rule.
59
List<CORSRule.AllowedMethods> rule3AM = new ArrayList<CORSRule.AllowedMethods>();
60
rule3AM.add(CORSRule.AllowedMethods.HEAD);
61
CORSRule rule3 = new CORSRule().withId("CORSRule3").withAllowedMethods(rule3AM)
62
.withAllowedOrigins(Arrays.asList(new String[] { "http://www.example.com" }));
63
64
rules = configuration.getRules();
65
rules.add(rule3);
66
configuration.setRules(rules);
67
s3Client.setBucketCrossOriginConfiguration(bucketName, configuration);
68
69
// Verify that the new rule was added by checking the number of rules in the configuration.
70
configuration = s3Client.getBucketCrossOriginConfiguration(bucketName);
71
System.out.println("Expected # of rules = 3, found " + configuration.getRules().size());
72
73
// Delete the configuration.
74
s3Client.deleteBucketCrossOriginConfiguration(bucketName);
75
System.out.println("Removed CORS configuration.");
76
77
// Retrieve and display the configuration to verify that it was
78
// successfully deleted.
79
configuration = s3Client.getBucketCrossOriginConfiguration(bucketName);
80
printCORSConfiguration(configuration);
81
}
82
catch(AmazonServiceException e) {
83
// The call was transmitted successfully, but Amazon S3 couldn't process
84
// it, so it returned an error response.
85
e.printStackTrace();
86
}
87
catch(SdkClientException e) {
88
// Amazon S3 couldn't be contacted for a response, or the client
89
// couldn't parse the response from Amazon S3.
90
e.printStackTrace();
91
}
92
}
93
94
private static void printCORSConfiguration(BucketCrossOriginConfiguration configuration) {
95
if (configuration == null) {
96
System.out.println("Configuration is null.");
97
} else {
98
System.out.println("Configuration has " + configuration.getRules().size() + " rules\n");
99
100
for (CORSRule rule : configuration.getRules()) {
101
System.out.println("Rule ID: " + rule.getId());
102
System.out.println("MaxAgeSeconds: " + rule.getMaxAgeSeconds());
103
System.out.println("AllowedMethod: " + rule.getAllowedMethods());
104
System.out.println("AllowedOrigins: " + rule.getAllowedOrigins());
105
System.out.println("AllowedHeaders: " + rule.getAllowedHeaders());
106
System.out.println("ExposeHeader: " + rule.getExposedHeaders());
107
System.out.println();
108
}
109
}
110
}
111
}
112