Search in sources :

Example 1 with Rule

use of com.amazonaws.services.s3.model.BucketLifecycleConfiguration.Rule in project BridgeServer2 by Sage-Bionetworks.

the class S3InitializerTest method bucketCreatedForPublicBucket.

@Test
public void bucketCreatedForPublicBucket() {
    Map<String, S3Initializer.BucketType> props = ImmutableMap.of("bucket.prop", S3Initializer.BucketType.PUBLIC_ACCESSIBLE);
    when(initializer.getBucketNames()).thenReturn(props);
    when(mockS3Client.doesBucketExistV2(BUCKET_NAME)).thenReturn(false);
    initializer.initBuckets();
    String resolvedPolicy = BridgeUtils.resolveTemplate(S3Initializer.BucketType.PUBLIC_ACCESSIBLE.policy, ImmutableMap.of("bucketName", BUCKET_NAME));
    verify(mockS3Client).createBucket(requestCaptor.capture());
    verify(mockS3Client).setBucketPolicy(BUCKET_NAME, resolvedPolicy);
    verify(mockS3Client).setBucketWebsiteConfiguration(eq(BUCKET_NAME), websiteConfigCaptor.capture());
    verify(mockS3Client).setBucketCrossOriginConfiguration(eq(BUCKET_NAME), corsConfigCaptor.capture());
    assertEquals(requestCaptor.getValue().getBucketName(), BUCKET_NAME);
    // Not a lot to check here, but
    BucketWebsiteConfiguration config = websiteConfigCaptor.getValue();
    assertEquals(config.getIndexDocumentSuffix(), "index.html");
    CORSRule rule = corsConfigCaptor.getValue().getRules().get(0);
    assertEquals(rule.getAllowedHeaders(), ImmutableList.of("*"));
    assertEquals(rule.getAllowedOrigins(), ImmutableList.of("*"));
    assertEquals(rule.getAllowedMethods(), ImmutableList.of(AllowedMethods.PUT));
    assertEquals(rule.getMaxAgeSeconds(), 3000);
}
Also used : BucketWebsiteConfiguration(com.amazonaws.services.s3.model.BucketWebsiteConfiguration) CORSRule(com.amazonaws.services.s3.model.CORSRule) Test(org.testng.annotations.Test)

Example 2 with Rule

use of com.amazonaws.services.s3.model.BucketLifecycleConfiguration.Rule in project BridgeServer2 by Sage-Bionetworks.

the class S3InitializerTest method assertCorsConfig.

private static void assertCorsConfig(BucketCrossOriginConfiguration corsConfig) {
    CORSRule rule = corsConfig.getRules().get(0);
    assertEquals(rule.getAllowedHeaders(), ImmutableList.of("*"));
    assertEquals(rule.getAllowedOrigins(), ImmutableList.of("*"));
    assertEquals(rule.getAllowedMethods(), ImmutableList.of(AllowedMethods.PUT));
    assertEquals(rule.getMaxAgeSeconds(), 3000);
}
Also used : CORSRule(com.amazonaws.services.s3.model.CORSRule)

Example 3 with Rule

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

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

the class CrossRegionReplication method main.

public static void main(String[] args) throws IOException {
    Regions clientRegion = Regions.DEFAULT_REGION;
    String accountId = "*** Account ID ***";
    String roleName = "*** Role name ***";
    String sourceBucketName = "*** Source bucket name ***";
    String destBucketName = "*** Destination bucket name ***";
    String prefix = "Tax/";
    String roleARN = String.format("arn:aws:iam::%s:role/%s", accountId, roleName);
    String destinationBucketARN = "arn:aws:s3:::" + destBucketName;
    AmazonS3 s3Client = AmazonS3Client.builder().withCredentials(new ProfileCredentialsProvider()).withRegion(clientRegion).build();
    createBucket(s3Client, clientRegion, sourceBucketName);
    createBucket(s3Client, clientRegion, destBucketName);
    assignRole(roleName, clientRegion, sourceBucketName, destBucketName);
    try {
        // Create the replication rule.
        List<ReplicationFilterPredicate> andOperands = new ArrayList<ReplicationFilterPredicate>();
        andOperands.add(new ReplicationPrefixPredicate(prefix));
        Map<String, ReplicationRule> replicationRules = new HashMap<String, ReplicationRule>();
        replicationRules.put("ReplicationRule1", new ReplicationRule().withPriority(0).withStatus(ReplicationRuleStatus.Enabled).withDeleteMarkerReplication(new DeleteMarkerReplication().withStatus(DeleteMarkerReplicationStatus.DISABLED)).withFilter(new ReplicationFilter().withPredicate(new ReplicationPrefixPredicate(prefix))).withDestinationConfig(new ReplicationDestinationConfig().withBucketARN(destinationBucketARN).withStorageClass(StorageClass.Standard)));
        // Save the replication rule to the source bucket.
        s3Client.setBucketReplicationConfiguration(sourceBucketName, new BucketReplicationConfiguration().withRoleARN(roleARN).withRules(replicationRules));
        // Retrieve the replication configuration and verify that the configuration
        // matches the rule we just set.
        BucketReplicationConfiguration replicationConfig = s3Client.getBucketReplicationConfiguration(sourceBucketName);
        ReplicationRule rule = replicationConfig.getRule("ReplicationRule1");
        System.out.println("Retrieved destination bucket ARN: " + rule.getDestinationConfig().getBucketARN());
        System.out.println("Retrieved priority: " + rule.getPriority());
        System.out.println("Retrieved source-bucket replication rule status: " + rule.getStatus());
    } 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 : ReplicationPrefixPredicate(com.amazonaws.services.s3.model.replication.ReplicationPrefixPredicate) AmazonS3(com.amazonaws.services.s3.AmazonS3) ReplicationDestinationConfig(com.amazonaws.services.s3.model.ReplicationDestinationConfig) BucketReplicationConfiguration(com.amazonaws.services.s3.model.BucketReplicationConfiguration) HashMap(java.util.HashMap) ReplicationFilterPredicate(com.amazonaws.services.s3.model.replication.ReplicationFilterPredicate) ArrayList(java.util.ArrayList) Regions(com.amazonaws.regions.Regions) ReplicationRule(com.amazonaws.services.s3.model.ReplicationRule) SdkClientException(com.amazonaws.SdkClientException) ReplicationFilter(com.amazonaws.services.s3.model.replication.ReplicationFilter) AmazonServiceException(com.amazonaws.AmazonServiceException) ProfileCredentialsProvider(com.amazonaws.auth.profile.ProfileCredentialsProvider) DeleteMarkerReplication(com.amazonaws.services.s3.model.DeleteMarkerReplication)

Example 5 with Rule

use of com.amazonaws.services.s3.model.BucketLifecycleConfiguration.Rule in project nexus-public by sonatype.

the class BucketManager method deleteStorageLocation.

@Override
public void deleteStorageLocation(final BlobStoreConfiguration blobStoreConfiguration) {
    String bucket = getConfiguredBucket(blobStoreConfiguration);
    ObjectListing listing = s3.listObjects(new ListObjectsRequest().withBucketName(bucket).withMaxKeys(1));
    if (listing.getObjectSummaries().isEmpty()) {
        s3.deleteBucket(bucket);
    } else {
        log.info("Not removing S3 bucket {} because it is not empty", bucket);
        BucketLifecycleConfiguration lifecycleConfiguration = s3.getBucketLifecycleConfiguration(bucket);
        List<Rule> nonBlobstoreRules = nonBlobstoreRules(lifecycleConfiguration, blobStoreConfiguration.getName());
        if (!isEmpty(nonBlobstoreRules)) {
            lifecycleConfiguration.setRules(nonBlobstoreRules);
            s3.setBucketLifecycleConfiguration(bucket, lifecycleConfiguration);
        } else {
            s3.deleteBucketLifecycleConfiguration(bucket);
        }
    }
}
Also used : ListObjectsRequest(com.amazonaws.services.s3.model.ListObjectsRequest) BucketLifecycleConfiguration(com.amazonaws.services.s3.model.BucketLifecycleConfiguration) ObjectListing(com.amazonaws.services.s3.model.ObjectListing) Rule(com.amazonaws.services.s3.model.BucketLifecycleConfiguration.Rule)

Aggregations

Test (org.junit.Test)15 Rule (com.amazonaws.services.s3.model.BucketLifecycleConfiguration.Rule)13 CORSRule (com.amazonaws.services.s3.model.CORSRule)7 BucketLifecycleConfiguration (com.amazonaws.services.s3.model.BucketLifecycleConfiguration)6 LifecycleTagPredicate (com.amazonaws.services.s3.model.lifecycle.LifecycleTagPredicate)6 LifecycleAndOperator (com.amazonaws.services.s3.model.lifecycle.LifecycleAndOperator)5 HttpResponse (com.amazonaws.http.HttpResponse)4 AmazonS3 (com.amazonaws.services.s3.AmazonS3)4 XmlWriter (com.amazonaws.services.s3.internal.XmlWriter)4 RedirectRule (com.amazonaws.services.s3.model.RedirectRule)4 ReplicationRule (com.amazonaws.services.s3.model.ReplicationRule)4 LifecycleFilter (com.amazonaws.services.s3.model.lifecycle.LifecycleFilter)4 LifecycleFilterPredicate (com.amazonaws.services.s3.model.lifecycle.LifecycleFilterPredicate)4 LifecyclePrefixPredicate (com.amazonaws.services.s3.model.lifecycle.LifecyclePrefixPredicate)4 AmazonServiceException (com.amazonaws.AmazonServiceException)3 SdkClientException (com.amazonaws.SdkClientException)3 ProfileCredentialsProvider (com.amazonaws.auth.profile.ProfileCredentialsProvider)3 Regions (com.amazonaws.regions.Regions)3 ObjectMetadata (com.amazonaws.services.s3.model.ObjectMetadata)3 ReplicationDestinationConfig (com.amazonaws.services.s3.model.ReplicationDestinationConfig)3