Search in sources :

Example 96 with AmazonS3Client

use of com.talend.shaded.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 97 with AmazonS3Client

use of com.talend.shaded.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 98 with AmazonS3Client

use of com.talend.shaded.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 99 with AmazonS3Client

use of com.talend.shaded.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)

Example 100 with AmazonS3Client

use of com.talend.shaded.com.amazonaws.services.s3.AmazonS3Client in project SimpleContentPortlet by Jasig.

the class AmazonS3PersistenceStrategy method persistAttachmentBinary.

@Override
public String persistAttachmentBinary(HttpServletRequest request, Attachment attachment) throws PersistenceException {
    // The data import is only going to update the attachment metadata in the database.
    if (request == null && attachment.getData() == null) {
        return null;
    }
    AmazonS3 s3 = new AmazonS3Client();
    String key = PATH_FORMAT.format(new Object[] { s3BucketPath, attachment.getGuid(), attachment.getFilename() });
    ObjectMetadata metadata = new ObjectMetadata();
    metadata.setContentType(attachment.getContentType());
    metadata.setContentLength(attachment.getData().length);
    metadata.setCacheControl(s3CacheControlString);
    // S3 chose base64-encoded hash not the typical 32-character hex string so convert accordingly.
    // Hex.decodeHex(attachment.getChecksum().toCharArray())
    metadata.setContentMD5(Base64.encodeBase64String(DatatypeConverter.parseHexBinary(attachment.getChecksum())));
    try {
        s3.putObject(new PutObjectRequest(s3BucketName, key, new ByteArrayInputStream(attachment.getData()), metadata));
        log.debug("Successfully sent {} to S3 bucket {} under key {}", attachment.getFilename(), s3BucketName, key);
    } catch (AmazonClientException e) {
        String message = String.format("Unable to persist attachment %1s to S3 bucket %2s, key %3s", attachment.getFilename(), s3BucketName, key);
        throw new PersistenceException(message, e);
    }
    return (s3BucketBaseUrl.endsWith("/") ? s3BucketBaseUrl : s3BucketBaseUrl + "/") + key;
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3) AmazonS3Client(com.amazonaws.services.s3.AmazonS3Client) ByteArrayInputStream(java.io.ByteArrayInputStream) AmazonClientException(com.amazonaws.AmazonClientException) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) PutObjectRequest(com.amazonaws.services.s3.model.PutObjectRequest)

Aggregations

AmazonS3Client (com.amazonaws.services.s3.AmazonS3Client)107 Test (org.junit.Test)23 BasicAWSCredentials (com.amazonaws.auth.BasicAWSCredentials)20 AmazonClientException (com.amazonaws.AmazonClientException)16 ClientConfiguration (com.amazonaws.ClientConfiguration)15 ArrayList (java.util.ArrayList)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 AWSCredentialsProvider (com.amazonaws.auth.AWSCredentialsProvider)10 ObjectMetadata (com.amazonaws.services.s3.model.ObjectMetadata)10 AmazonServiceException (com.amazonaws.AmazonServiceException)9 AmazonS3Exception (com.amazonaws.services.s3.model.AmazonS3Exception)9 InternalEvent (com.nextdoor.bender.InternalEvent)9 TestContext (com.nextdoor.bender.aws.TestContext)9 IOException (java.io.IOException)9