Search in sources :

Example 91 with AmazonS3Client

use of com.amazonaws.services.s3.AmazonS3Client in project photon-model by vmware.

the class TestAWSClientManagement method testAwsS3ClientManagement.

@Test
public void testAwsS3ClientManagement() throws Throwable {
    // Ensure that we start with a clean state.
    AWSClientManagerFactory.cleanUp(AwsClientType.S3_TRANSFER_MANAGER);
    // Get a reference to the client manager in the test
    AWSClientManager s3ClientManager = getClientManager(AwsClientType.S3_TRANSFER_MANAGER);
    assertEquals(1, getClientReferenceCount(AwsClientType.S3_TRANSFER_MANAGER));
    AuthCredentialsServiceState testCreds = new AuthCredentialsServiceState();
    testCreds.privateKey = this.accessKey;
    testCreds.privateKeyId = this.secretKey;
    final AmazonS3Client[] s3Client = new AmazonS3Client[1];
    TestContext waitContext = new TestContext(1, Duration.ofSeconds(30L));
    s3ClientManager.getOrCreateS3TransferManagerAsync(testCreds, TestAWSSetupUtils.regionId, this.instanceService).exceptionally(t -> {
        waitContext.fail(t);
        throw new CompletionException(t);
    }).thenAccept(i -> waitContext.complete());
    waitContext.await();
    assertEquals(1, s3ClientManager.getCacheCount());
    // Return the references from the test
    returnClientManager(s3ClientManager, AwsClientType.S3_TRANSFER_MANAGER);
    assertEquals(0, getClientReferenceCount(AwsClientType.S3_TRANSFER_MANAGER));
}
Also used : AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) BeforeClass(org.junit.BeforeClass) HashMap(java.util.HashMap) AWSClientManagerFactory.returnClientManager(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSClientManagerFactory.returnClientManager) AWSClientManagerFactory.getClientManager(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSClientManagerFactory.getClientManager) AWSClientManagerFactory.getClientReferenceCount(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSClientManagerFactory.getClientReferenceCount) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) CommandLineArgumentParser(com.vmware.xenon.common.CommandLineArgumentParser) AWSSecurityTokenServiceException(com.amazonaws.services.securitytoken.model.AWSSecurityTokenServiceException) Duration(java.time.Duration) After(org.junit.After) EXTERNAL_ID_KEY(com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest.EXTERNAL_ID_KEY) AWS_ARN_DEFAULT_SESSION_DURATION_SECONDS_PROPERTY(com.vmware.photon.controller.model.adapters.awsadapter.AWSUtils.AWS_ARN_DEFAULT_SESSION_DURATION_SECONDS_PROPERTY) AwsClientType(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AwsClientType) BasicReusableHostTestCase(com.vmware.xenon.common.BasicReusableHostTestCase) AWSClientManager(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSClientManager) Before(org.junit.Before) StatelessService(com.vmware.xenon.common.StatelessService) AWS_MASTER_ACCOUNT_ACCESS_KEY_PROPERTY(com.vmware.photon.controller.model.adapters.awsadapter.AWSUtils.AWS_MASTER_ACCOUNT_ACCESS_KEY_PROPERTY) Operation(com.vmware.xenon.common.Operation) CompletionException(java.util.concurrent.CompletionException) Test(org.junit.Test) AmazonS3Client(com.amazonaws.services.s3.AmazonS3Client) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) AWS_MASTER_ACCOUNT_SECRET_KEY_PROPERTY(com.vmware.photon.controller.model.adapters.awsadapter.AWSUtils.AWS_MASTER_ACCOUNT_SECRET_KEY_PROPERTY) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) TestContext(com.vmware.xenon.common.test.TestContext) Ignore(org.junit.Ignore) ARN_KEY(com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest.ARN_KEY) UriUtils(com.vmware.xenon.common.UriUtils) Assume.assumeTrue(org.junit.Assume.assumeTrue) AWSClientManagerFactory(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSClientManagerFactory) Assert(org.junit.Assert) Assert.assertEquals(org.junit.Assert.assertEquals) AmazonEC2AsyncClient(com.amazonaws.services.ec2.AmazonEC2AsyncClient) AWSClientManager(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSClientManager) AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) AmazonS3Client(com.amazonaws.services.s3.AmazonS3Client) TestContext(com.vmware.xenon.common.test.TestContext) CompletionException(java.util.concurrent.CompletionException) Test(org.junit.Test)

Example 92 with AmazonS3Client

use of com.amazonaws.services.s3.AmazonS3Client in project wildfly-camel by wildfly-extras.

the class S3Utils method createS3Client.

// Attach Policy: AmazonS3FullAccess
public static AmazonS3Client createS3Client() {
    BasicCredentialsProvider credentials = BasicCredentialsProvider.standard();
    AmazonS3Client client = !credentials.isValid() ? null : (AmazonS3Client) AmazonS3ClientBuilder.standard().withCredentials(credentials).withRegion("eu-west-1").build();
    return client;
}
Also used : AmazonS3Client(com.amazonaws.services.s3.AmazonS3Client)

Example 93 with AmazonS3Client

use of com.amazonaws.services.s3.AmazonS3Client in project wildfly-camel by wildfly-extras.

the class S3Utils method createBucket.

@SuppressWarnings("unchecked")
public static void createBucket(AmazonS3Client client, String bucketName) throws Exception {
    client.createBucket(bucketName);
    HeadBucketRequest request = new HeadBucketRequest(bucketName);
    Waiter<HeadBucketRequest> waiter = client.waiters().bucketExists();
    Future<Void> future = waiter.runAsync(new WaiterParameters<HeadBucketRequest>(request), new NoOpWaiterHandler());
    future.get(1, TimeUnit.MINUTES);
}
Also used : HeadBucketRequest(com.amazonaws.services.s3.model.HeadBucketRequest) NoOpWaiterHandler(com.amazonaws.waiters.NoOpWaiterHandler)

Example 94 with AmazonS3Client

use of com.amazonaws.services.s3.AmazonS3Client in project wildfly-camel by wildfly-extras.

the class S3IntegrationTest method testBucketOperations.

@Test
public void testBucketOperations() throws Exception {
    AmazonS3Client s3Client = provider.getClient();
    Assume.assumeNotNull("AWS client not null", s3Client);
    assertNoStaleBuckets(s3Client, "before");
    try {
        try {
            S3Utils.createBucket(s3Client, bucketName);
            WildFlyCamelContext camelctx = new WildFlyCamelContext();
            camelctx.getNamingContext().bind("s3Client", s3Client);
            camelctx.addRoutes(new RouteBuilder() {

                @Override
                public void configure() throws Exception {
                    String clientref = "amazonS3Client=#s3Client";
                    from("direct:upload").to("aws-s3://" + bucketName + "?" + clientref);
                    from("aws-s3://" + bucketName + "?" + clientref).to("seda:read");
                }
            });
            try {
                camelctx.start();
                try {
                    // Put object
                    Map<String, Object> headers = new HashMap<>();
                    headers.put(S3Constants.KEY, OBJECT_KEY);
                    ProducerTemplate producer = camelctx.createProducerTemplate();
                    String content = "My bucket content";
                    String result1 = producer.requestBodyAndHeaders("direct:upload", content, headers, String.class);
                    Assert.assertEquals(content, result1);
                    ConsumerTemplate consumer = camelctx.createConsumerTemplate();
                    String result2 = consumer.receiveBody("seda:read", String.class);
                    Assert.assertEquals(content, result2);
                } finally {
                    camelctx.stop();
                }
            } finally {
                s3Client.deleteObject(bucketName, OBJECT_KEY);
            }
        } finally {
            S3Utils.deleteBucket(s3Client, bucketName);
        }
    } finally {
        assertNoStaleBuckets(s3Client, "after");
    }
}
Also used : ProducerTemplate(org.apache.camel.ProducerTemplate) ConsumerTemplate(org.apache.camel.ConsumerTemplate) AmazonS3Client(com.amazonaws.services.s3.AmazonS3Client) RouteBuilder(org.apache.camel.builder.RouteBuilder) HashMap(java.util.HashMap) WildFlyCamelContext(org.wildfly.extension.camel.WildFlyCamelContext) Test(org.junit.Test)

Example 95 with AmazonS3Client

use of com.amazonaws.services.s3.AmazonS3Client in project opentest by mcdcorp.

the class GetS3Metadata method run.

@Override
public void run() {
    super.run();
    String awsCredentialsProfile = this.readStringArgument("awsProfile", "default");
    String bucket = this.readStringArgument("bucket");
    String objectKey = this.readStringArgument("objectKey");
    AmazonS3 s3Client = new AmazonS3Client(new ProfileCredentialsProvider(awsCredentialsProfile));
    ObjectMetadata metadata = s3Client.getObjectMetadata(new GetObjectMetadataRequest(bucket, objectKey));
    try {
        Date expirationTime = metadata.getExpirationTime();
        if (expirationTime != null) {
            this.writeOutput("expirationTime", metadata.getExpirationTime().getTime());
        } else {
            this.writeOutput("expirationTime", null);
        }
        this.writeOutput("lastModified", metadata.getLastModified().getTime());
        this.writeOutput("userMetadata", metadata.getUserMetadata());
        this.writeOutput("size", metadata.getContentLength());
        this.writeOutput("storageClass", metadata.getStorageClass());
        this.writeOutput("versionId", metadata.getVersionId());
    } catch (Exception ex) {
        throw new RuntimeException(String.format("Failed to get object metadata for object key %s in bucket %s", objectKey, bucket), ex);
    }
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3) AmazonS3Client(com.amazonaws.services.s3.AmazonS3Client) GetObjectMetadataRequest(com.amazonaws.services.s3.model.GetObjectMetadataRequest) ProfileCredentialsProvider(com.amazonaws.auth.profile.ProfileCredentialsProvider) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) Date(java.util.Date)

Aggregations

AmazonS3Client (com.amazonaws.services.s3.AmazonS3Client)109 Test (org.junit.Test)23 BasicAWSCredentials (com.amazonaws.auth.BasicAWSCredentials)20 AmazonClientException (com.amazonaws.AmazonClientException)18 ClientConfiguration (com.amazonaws.ClientConfiguration)18 ArrayList (java.util.ArrayList)14 AWSCredentialsProvider (com.amazonaws.auth.AWSCredentialsProvider)13 HashMap (java.util.HashMap)13 AmazonS3 (com.amazonaws.services.s3.AmazonS3)12 File (java.io.File)12 InvocationOnMock (org.mockito.invocation.InvocationOnMock)12 PutObjectResult (com.amazonaws.services.s3.model.PutObjectResult)11 UploadPartRequest (com.amazonaws.services.s3.model.UploadPartRequest)11 AWSCredentials (com.amazonaws.auth.AWSCredentials)10 AmazonS3Exception (com.amazonaws.services.s3.model.AmazonS3Exception)10 ObjectMetadata (com.amazonaws.services.s3.model.ObjectMetadata)10 S3Object (com.amazonaws.services.s3.model.S3Object)9 InternalEvent (com.nextdoor.bender.InternalEvent)9 TestContext (com.nextdoor.bender.aws.TestContext)9 IOException (java.io.IOException)9