Search in sources :

Example 1 with CORSConfiguration

use of software.amazon.awssdk.services.s3.model.CORSConfiguration in project aws-doc-sdk-examples by awsdocs.

the class S3Cors method setCorsInformation.

public static void setCorsInformation(S3Client s3, String bucketName, String accountId) {
    List<String> allowMethods = new ArrayList();
    allowMethods.add("PUT");
    allowMethods.add("POST");
    allowMethods.add("DELETE");
    List<String> allowOrigins = new ArrayList();
    allowOrigins.add("http://example.com");
    try {
        // Define CORS rules.
        CORSRule corsRule = CORSRule.builder().allowedMethods(allowMethods).allowedOrigins(allowOrigins).build();
        List<CORSRule> corsRules = new ArrayList<>();
        corsRules.add(corsRule);
        CORSConfiguration configuration = CORSConfiguration.builder().corsRules(corsRules).build();
        PutBucketCorsRequest putBucketCorsRequest = PutBucketCorsRequest.builder().bucket(bucketName).corsConfiguration(configuration).expectedBucketOwner(accountId).build();
        s3.putBucketCors(putBucketCorsRequest);
    } catch (S3Exception e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
}
Also used : CORSConfiguration(software.amazon.awssdk.services.s3.model.CORSConfiguration) PutBucketCorsRequest(software.amazon.awssdk.services.s3.model.PutBucketCorsRequest) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) ArrayList(java.util.ArrayList) CORSRule(software.amazon.awssdk.services.s3.model.CORSRule)

Aggregations

ArrayList (java.util.ArrayList)1 CORSConfiguration (software.amazon.awssdk.services.s3.model.CORSConfiguration)1 CORSRule (software.amazon.awssdk.services.s3.model.CORSRule)1 PutBucketCorsRequest (software.amazon.awssdk.services.s3.model.PutBucketCorsRequest)1 S3Exception (software.amazon.awssdk.services.s3.model.S3Exception)1