Search in sources :

Example 1 with PutObjectRequest

use of com.amazonaws.services.s3.model.PutObjectRequest in project camel by apache.

the class AmazonS3ClientMock method putObject.

@SuppressWarnings("resource")
@Override
public PutObjectResult putObject(PutObjectRequest putObjectRequest) throws AmazonClientException, AmazonServiceException {
    putObjectRequests.add(putObjectRequest);
    S3Object s3Object = new S3Object();
    s3Object.setBucketName(putObjectRequest.getBucketName());
    s3Object.setKey(putObjectRequest.getKey());
    if (putObjectRequest.getFile() != null) {
        try {
            s3Object.setObjectContent(new FileInputStream(putObjectRequest.getFile()));
        } catch (FileNotFoundException e) {
            throw new AmazonServiceException("Cannot store the file object.", e);
        }
    } else {
        s3Object.setObjectContent(putObjectRequest.getInputStream());
    }
    objects.add(s3Object);
    PutObjectResult putObjectResult = new PutObjectResult();
    putObjectResult.setETag("3a5c8b1ad448bca04584ecb55b836264");
    return putObjectResult;
}
Also used : PutObjectResult(com.amazonaws.services.s3.model.PutObjectResult) FileNotFoundException(java.io.FileNotFoundException) AmazonServiceException(com.amazonaws.AmazonServiceException) S3Object(com.amazonaws.services.s3.model.S3Object) FileInputStream(java.io.FileInputStream)

Example 2 with PutObjectRequest

use of com.amazonaws.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 3 with PutObjectRequest

use of com.amazonaws.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 4 with PutObjectRequest

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

Example 5 with PutObjectRequest

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

Aggregations

PutObjectRequest (com.amazonaws.services.s3.model.PutObjectRequest)33 ObjectMetadata (com.amazonaws.services.s3.model.ObjectMetadata)21 Upload (com.amazonaws.services.s3.transfer.Upload)11 AmazonClientException (com.amazonaws.AmazonClientException)10 PutObjectResult (com.amazonaws.services.s3.model.PutObjectResult)8 Exchange (org.apache.camel.Exchange)8 Processor (org.apache.camel.Processor)8 Test (org.junit.Test)8 InputStream (java.io.InputStream)7 DataStoreException (org.apache.jackrabbit.core.data.DataStoreException)7 File (java.io.File)6 IOException (java.io.IOException)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 AmazonServiceException (com.amazonaws.AmazonServiceException)4 S3Object (com.amazonaws.services.s3.model.S3Object)4 Date (java.util.Date)4 CopyObjectRequest (com.amazonaws.services.s3.model.CopyObjectRequest)3 Copy (com.amazonaws.services.s3.transfer.Copy)3 FileInputStream (java.io.FileInputStream)3 HashMap (java.util.HashMap)3