Search in sources :

Example 1 with BucketCrossOriginConfiguration

use of com.amazonaws.services.s3.model.BucketCrossOriginConfiguration in project aws-doc-sdk-examples by awsdocs.

the class CORS method main.

public static void main(String[] args) throws IOException {
    Regions clientRegion = Regions.DEFAULT_REGION;
    String bucketName = "*** Bucket name ***";
    // Create two CORS rules.
    List<CORSRule.AllowedMethods> rule1AM = new ArrayList<CORSRule.AllowedMethods>();
    rule1AM.add(CORSRule.AllowedMethods.PUT);
    rule1AM.add(CORSRule.AllowedMethods.POST);
    rule1AM.add(CORSRule.AllowedMethods.DELETE);
    CORSRule rule1 = new CORSRule().withId("CORSRule1").withAllowedMethods(rule1AM).withAllowedOrigins(Arrays.asList("http://*.example.com"));
    List<CORSRule.AllowedMethods> rule2AM = new ArrayList<CORSRule.AllowedMethods>();
    rule2AM.add(CORSRule.AllowedMethods.GET);
    CORSRule rule2 = new CORSRule().withId("CORSRule2").withAllowedMethods(rule2AM).withAllowedOrigins(Arrays.asList("*")).withMaxAgeSeconds(3000).withExposedHeaders(Arrays.asList("x-amz-server-side-encryption"));
    List<CORSRule> rules = new ArrayList<CORSRule>();
    rules.add(rule1);
    rules.add(rule2);
    // Add the rules to a new CORS configuration.
    BucketCrossOriginConfiguration configuration = new BucketCrossOriginConfiguration();
    configuration.setRules(rules);
    try {
        AmazonS3 s3Client = AmazonS3ClientBuilder.standard().withCredentials(new ProfileCredentialsProvider()).withRegion(clientRegion).build();
        // Add the configuration to the bucket.
        s3Client.setBucketCrossOriginConfiguration(bucketName, configuration);
        // Retrieve and display the configuration.
        configuration = s3Client.getBucketCrossOriginConfiguration(bucketName);
        printCORSConfiguration(configuration);
        // Add another new rule.
        List<CORSRule.AllowedMethods> rule3AM = new ArrayList<CORSRule.AllowedMethods>();
        rule3AM.add(CORSRule.AllowedMethods.HEAD);
        CORSRule rule3 = new CORSRule().withId("CORSRule3").withAllowedMethods(rule3AM).withAllowedOrigins(Arrays.asList("http://www.example.com"));
        rules = configuration.getRules();
        rules.add(rule3);
        configuration.setRules(rules);
        s3Client.setBucketCrossOriginConfiguration(bucketName, configuration);
        // Verify that the new rule was added by checking the number of rules in the configuration.
        configuration = s3Client.getBucketCrossOriginConfiguration(bucketName);
        System.out.println("Expected # of rules = 3, found " + configuration.getRules().size());
        // Delete the configuration.
        s3Client.deleteBucketCrossOriginConfiguration(bucketName);
        System.out.println("Removed CORS configuration.");
        // Retrieve and display the configuration to verify that it was
        // successfully deleted.
        configuration = s3Client.getBucketCrossOriginConfiguration(bucketName);
        printCORSConfiguration(configuration);
    } catch (AmazonServiceException e) {
        // The call was transmitted successfully, but Amazon S3 couldn't process
        // it, so it returned an error response.
        e.printStackTrace();
    } catch (SdkClientException e) {
        // Amazon S3 couldn't be contacted for a response, or the client
        // couldn't parse the response from Amazon S3.
        e.printStackTrace();
    }
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3) BucketCrossOriginConfiguration(com.amazonaws.services.s3.model.BucketCrossOriginConfiguration) SdkClientException(com.amazonaws.SdkClientException) ArrayList(java.util.ArrayList) CORSRule(com.amazonaws.services.s3.model.CORSRule) AmazonServiceException(com.amazonaws.AmazonServiceException) ProfileCredentialsProvider(com.amazonaws.auth.profile.ProfileCredentialsProvider) Regions(com.amazonaws.regions.Regions)

Example 2 with BucketCrossOriginConfiguration

use of com.amazonaws.services.s3.model.BucketCrossOriginConfiguration in project aws-doc-sdk-examples by awsdocs.

the class CORS method printCORSConfiguration.

private static void printCORSConfiguration(BucketCrossOriginConfiguration configuration) {
    if (configuration == null) {
        System.out.println("Configuration is null.");
    } else {
        System.out.println("Configuration has " + configuration.getRules().size() + " rules\n");
        for (CORSRule rule : configuration.getRules()) {
            System.out.println("Rule ID: " + rule.getId());
            System.out.println("MaxAgeSeconds: " + rule.getMaxAgeSeconds());
            System.out.println("AllowedMethod: " + rule.getAllowedMethods());
            System.out.println("AllowedOrigins: " + rule.getAllowedOrigins());
            System.out.println("AllowedHeaders: " + rule.getAllowedHeaders());
            System.out.println("ExposeHeader: " + rule.getExposedHeaders());
            System.out.println();
        }
    }
}
Also used : CORSRule(com.amazonaws.services.s3.model.CORSRule)

Aggregations

CORSRule (com.amazonaws.services.s3.model.CORSRule)2 AmazonServiceException (com.amazonaws.AmazonServiceException)1 SdkClientException (com.amazonaws.SdkClientException)1 ProfileCredentialsProvider (com.amazonaws.auth.profile.ProfileCredentialsProvider)1 Regions (com.amazonaws.regions.Regions)1 AmazonS3 (com.amazonaws.services.s3.AmazonS3)1 BucketCrossOriginConfiguration (com.amazonaws.services.s3.model.BucketCrossOriginConfiguration)1 ArrayList (java.util.ArrayList)1