Search in sources :

Example 1 with CORSRule

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

the class S3Cors method getBucketCorsInformation.

public static void getBucketCorsInformation(S3Client s3, String bucketName, String accountId) {
    try {
        GetBucketCorsRequest bucketCorsRequest = GetBucketCorsRequest.builder().bucket(bucketName).expectedBucketOwner(accountId).build();
        GetBucketCorsResponse corsResponse = s3.getBucketCors(bucketCorsRequest);
        List<CORSRule> corsRules = corsResponse.corsRules();
        for (CORSRule rule : corsRules) {
            System.out.println("allowOrigins: " + rule.allowedOrigins());
            System.out.println("AllowedMethod: " + rule.allowedMethods());
        }
    } catch (S3Exception e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
}
Also used : GetBucketCorsRequest(software.amazon.awssdk.services.s3.model.GetBucketCorsRequest) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) CORSRule(software.amazon.awssdk.services.s3.model.CORSRule) GetBucketCorsResponse(software.amazon.awssdk.services.s3.model.GetBucketCorsResponse)

Example 2 with CORSRule

use of com.amazonaws.services.s3.model.CORSRule 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)

Example 3 with CORSRule

use of com.amazonaws.services.s3.model.CORSRule 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 4 with CORSRule

use of com.amazonaws.services.s3.model.CORSRule 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 ArrayList (java.util.ArrayList)2 CORSRule (software.amazon.awssdk.services.s3.model.CORSRule)2 S3Exception (software.amazon.awssdk.services.s3.model.S3Exception)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 CORSConfiguration (software.amazon.awssdk.services.s3.model.CORSConfiguration)1 GetBucketCorsRequest (software.amazon.awssdk.services.s3.model.GetBucketCorsRequest)1 GetBucketCorsResponse (software.amazon.awssdk.services.s3.model.GetBucketCorsResponse)1 PutBucketCorsRequest (software.amazon.awssdk.services.s3.model.PutBucketCorsRequest)1