Search in sources :

Example 6 with PutObjectRequest

use of software.amazon.awssdk.services.s3.model.PutObjectRequest in project gradle by gradle.

the class S3Client method put.

public void put(InputStream inputStream, Long contentLength, URI destination) {
    checkRequiredJigsawModuleIsOnPath();
    try {
        S3RegionalResource s3RegionalResource = new S3RegionalResource(destination);
        String bucketName = s3RegionalResource.getBucketName();
        String s3BucketKey = s3RegionalResource.getKey();
        configureClient(s3RegionalResource);
        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setContentLength(contentLength);
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, s3BucketKey, inputStream, objectMetadata);
        LOGGER.debug("Attempting to put resource:[{}] into s3 bucket [{}]", s3BucketKey, bucketName);
        amazonS3Client.putObject(putObjectRequest);
    } catch (AmazonClientException e) {
        throw ResourceExceptions.putFailed(destination, e);
    }
}
Also used : AmazonClientException(com.amazonaws.AmazonClientException) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) PutObjectRequest(com.amazonaws.services.s3.model.PutObjectRequest)

Example 7 with PutObjectRequest

use of software.amazon.awssdk.services.s3.model.PutObjectRequest in project camel by apache.

the class S3Producer method processSingleOp.

public void processSingleOp(final Exchange exchange) throws Exception {
    ObjectMetadata objectMetadata = determineMetadata(exchange);
    File filePayload = null;
    InputStream is = null;
    Object obj = exchange.getIn().getMandatoryBody();
    PutObjectRequest putObjectRequest = null;
    // Need to check if the message body is WrappedFile
    if (obj instanceof WrappedFile) {
        obj = ((WrappedFile<?>) obj).getFile();
    }
    if (obj instanceof File) {
        filePayload = (File) obj;
        is = new FileInputStream(filePayload);
    } else {
        is = exchange.getIn().getMandatoryBody(InputStream.class);
    }
    putObjectRequest = new PutObjectRequest(getConfiguration().getBucketName(), determineKey(exchange), is, objectMetadata);
    String storageClass = determineStorageClass(exchange);
    if (storageClass != null) {
        putObjectRequest.setStorageClass(storageClass);
    }
    String cannedAcl = exchange.getIn().getHeader(S3Constants.CANNED_ACL, String.class);
    if (cannedAcl != null) {
        CannedAccessControlList objectAcl = CannedAccessControlList.valueOf(cannedAcl);
        putObjectRequest.setCannedAcl(objectAcl);
    }
    AccessControlList acl = exchange.getIn().getHeader(S3Constants.ACL, AccessControlList.class);
    if (acl != null) {
        // note: if cannedacl and acl are both specified the last one will be used. refer to
        // PutObjectRequest#setAccessControlList for more details
        putObjectRequest.setAccessControlList(acl);
    }
    LOG.trace("Put object [{}] from exchange [{}]...", putObjectRequest, exchange);
    PutObjectResult putObjectResult = getEndpoint().getS3Client().putObject(putObjectRequest);
    LOG.trace("Received result [{}]", putObjectResult);
    Message message = getMessageForResponse(exchange);
    message.setHeader(S3Constants.E_TAG, putObjectResult.getETag());
    if (putObjectResult.getVersionId() != null) {
        message.setHeader(S3Constants.VERSION_ID, putObjectResult.getVersionId());
    }
    if (getConfiguration().isDeleteAfterWrite() && filePayload != null) {
        // close streams
        IOHelper.close(putObjectRequest.getInputStream());
        IOHelper.close(is);
        FileUtil.deleteFile(filePayload);
    }
}
Also used : CannedAccessControlList(com.amazonaws.services.s3.model.CannedAccessControlList) AccessControlList(com.amazonaws.services.s3.model.AccessControlList) Message(org.apache.camel.Message) WrappedFile(org.apache.camel.WrappedFile) PutObjectResult(com.amazonaws.services.s3.model.PutObjectResult) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) CannedAccessControlList(com.amazonaws.services.s3.model.CannedAccessControlList) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) File(java.io.File) WrappedFile(org.apache.camel.WrappedFile) PutObjectRequest(com.amazonaws.services.s3.model.PutObjectRequest) FileInputStream(java.io.FileInputStream)

Example 8 with PutObjectRequest

use of software.amazon.awssdk.services.s3.model.PutObjectRequest in project camel by apache.

the class S3ComponentNonExistingBucketTest method sendCustomHeaderValues.

@Test
public void sendCustomHeaderValues() throws Exception {
    result.expectedMessageCount(1);
    final Date now = new Date();
    final Map<String, String> s3Headers = new HashMap<String, String>();
    s3Headers.put("x-aws-s3-header", "extra");
    Exchange exchange = template.send("direct:start", ExchangePattern.InOnly, new Processor() {

        public void process(Exchange exchange) throws Exception {
            exchange.getIn().setHeader(S3Constants.STORAGE_CLASS, "STANDARD");
            exchange.getIn().setHeader(S3Constants.KEY, "CamelUnitTest");
            exchange.getIn().setHeader(S3Constants.CONTENT_LENGTH, 2L);
            exchange.getIn().setHeader(S3Constants.CONTENT_TYPE, "text/html");
            exchange.getIn().setHeader(S3Constants.CACHE_CONTROL, "no-cache");
            exchange.getIn().setHeader(S3Constants.CONTENT_DISPOSITION, "attachment;");
            exchange.getIn().setHeader(S3Constants.CONTENT_ENCODING, "gzip");
            exchange.getIn().setHeader(S3Constants.CONTENT_MD5, "TWF");
            exchange.getIn().setHeader(S3Constants.LAST_MODIFIED, now);
            exchange.getIn().setHeader(S3Constants.S3_HEADERS, s3Headers);
            exchange.getIn().setBody("This is my bucket content.");
        }
    });
    assertMockEndpointsSatisfied();
    assertResultExchange(result.getExchanges().get(0));
    PutObjectRequest putObjectRequest = client.putObjectRequests.get(0);
    assertEquals("STANDARD", putObjectRequest.getStorageClass());
    assertEquals("nonExistingBucket", putObjectRequest.getBucketName());
    assertEquals(2L, putObjectRequest.getMetadata().getContentLength());
    assertEquals("text/html", putObjectRequest.getMetadata().getContentType());
    assertEquals("no-cache", putObjectRequest.getMetadata().getCacheControl());
    assertEquals("attachment;", putObjectRequest.getMetadata().getContentDisposition());
    assertEquals("gzip", putObjectRequest.getMetadata().getContentEncoding());
    assertEquals("TWF", putObjectRequest.getMetadata().getContentMD5());
    assertEquals(now, putObjectRequest.getMetadata().getLastModified());
    assertEquals("extra", putObjectRequest.getMetadata().getRawMetadataValue("x-aws-s3-header"));
    assertResponseMessage(exchange.getIn());
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) HashMap(java.util.HashMap) Date(java.util.Date) PutObjectRequest(com.amazonaws.services.s3.model.PutObjectRequest) Test(org.junit.Test)

Example 9 with PutObjectRequest

use of software.amazon.awssdk.services.s3.model.PutObjectRequest in project camel by apache.

the class S3ComponentExistingBucketTest method sendIn.

@Test
public void sendIn() throws Exception {
    result.expectedMessageCount(1);
    Exchange exchange = template.send("direct:start", ExchangePattern.InOnly, new Processor() {

        public void process(Exchange exchange) throws Exception {
            exchange.getIn().setHeader(S3Constants.KEY, "CamelUnitTest");
            exchange.getIn().setBody("This is my bucket content.");
        }
    });
    assertMockEndpointsSatisfied();
    assertResultExchange(result.getExchanges().get(0));
    PutObjectRequest putObjectRequest = client.putObjectRequests.get(0);
    assertEquals("REDUCED_REDUNDANCY", putObjectRequest.getStorageClass());
    assertEquals("mycamelbucket", putObjectRequest.getBucketName());
    assertResponseMessage(exchange.getIn());
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) PutObjectRequest(com.amazonaws.services.s3.model.PutObjectRequest) Test(org.junit.Test)

Example 10 with PutObjectRequest

use of software.amazon.awssdk.services.s3.model.PutObjectRequest in project camel by apache.

the class S3ComponentFileDeleteTest method sendFile.

@Test
@Override
public void sendFile() throws Exception {
    result.expectedMessageCount(1);
    Exchange exchange = templateDelete.send("direct:startDelete", ExchangePattern.InOnly, new Processor() {

        public void process(Exchange exchange) throws Exception {
            exchange.getIn().setHeader(S3Constants.KEY, "CamelUnitTest");
            exchange.getIn().setBody(testFile);
        }
    });
    assertMockEndpointsSatisfied();
    assertResultExchange(result.getExchanges().get(0), true);
    PutObjectRequest putObjectRequest = client.putObjectRequests.get(0);
    assertEquals(getCamelBucket(), putObjectRequest.getBucketName());
    assertResponseMessage(exchange.getIn());
    assertFileNotExists(testFile.getAbsolutePath());
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) PutObjectRequest(com.amazonaws.services.s3.model.PutObjectRequest) Test(org.junit.Test)

Aggregations

PutObjectRequest (com.amazonaws.services.s3.model.PutObjectRequest)140 ObjectMetadata (com.amazonaws.services.s3.model.ObjectMetadata)80 ByteArrayInputStream (java.io.ByteArrayInputStream)63 Test (org.junit.Test)61 File (java.io.File)35 IOException (java.io.IOException)32 S3FileTransferRequestParamsDto (org.finra.herd.model.dto.S3FileTransferRequestParamsDto)29 PutObjectResult (com.amazonaws.services.s3.model.PutObjectResult)23 Upload (com.amazonaws.services.s3.transfer.Upload)23 AmazonClientException (com.amazonaws.AmazonClientException)20 InputStream (java.io.InputStream)19 PutObjectRequest (software.amazon.awssdk.services.s3.model.PutObjectRequest)15 BusinessObjectDataKey (org.finra.herd.model.api.xml.BusinessObjectDataKey)14 StorageUnitEntity (org.finra.herd.model.jpa.StorageUnitEntity)14 BusinessObjectDataEntity (org.finra.herd.model.jpa.BusinessObjectDataEntity)13 HashMap (java.util.HashMap)12 S3Exception (software.amazon.awssdk.services.s3.model.S3Exception)10 AmazonServiceException (com.amazonaws.AmazonServiceException)9 AmazonS3 (com.amazonaws.services.s3.AmazonS3)9 Exchange (org.apache.camel.Exchange)8