Search in sources :

Example 86 with PutObjectRequest

use of software.amazon.awssdk.services.s3.model.PutObjectRequest in project gradle-s3-build-cache by myniva.

the class AwsS3BuildCacheService method store.

@Override
public void store(BuildCacheKey key, BuildCacheEntryWriter writer) {
    final String bucketPath = getBucketPath(key);
    logger.info("Start storing cache entry '{}' in S3 bucket", bucketPath);
    ObjectMetadata meta = new ObjectMetadata();
    meta.setContentType(BUILD_CACHE_CONTENT_TYPE);
    try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
        writer.writeTo(os);
        meta.setContentLength(os.size());
        try (InputStream is = new ByteArrayInputStream(os.toByteArray())) {
            PutObjectRequest request = getPutObjectRequest(bucketPath, meta, is);
            if (this.reducedRedundancy) {
                request.withStorageClass(StorageClass.ReducedRedundancy);
            }
            s3.putObject(request);
        }
    } catch (IOException e) {
        throw new BuildCacheException("Error while storing cache object in S3 bucket", e);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) BuildCacheException(org.gradle.caching.BuildCacheException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) PutObjectRequest(com.amazonaws.services.s3.model.PutObjectRequest)

Example 87 with PutObjectRequest

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

the class S3Client method writeToStorage.

/**
  * Upload a new object to S3 or update an object on S3.
  *
  * @param bucket
  *            The name of the bucket
  * @param key
  *            The file key of the object to upload/update.
  * @param values
  *            The data to be written on the object
  * @param updateMarker
  *            A boolean value. If true a new object will be uploaded
  *            to S3. If false an existing object will be re-uploaded
  *
  */
protected Status writeToStorage(String bucket, String key, HashMap<String, ByteIterator> values, Boolean updateMarker, String sseLocal, SSECustomerKey ssecLocal) {
    int totalSize = 0;
    //number of fields to concatenate
    int fieldCount = values.size();
    // getting the first field in the values
    Object keyToSearch = values.keySet().toArray()[0];
    // getting the content of just one field
    byte[] sourceArray = values.get(keyToSearch).toArray();
    //size of each array
    int sizeArray = sourceArray.length;
    if (updateMarker) {
        totalSize = sizeArray * fieldCount;
    } else {
        try {
            Map.Entry<S3Object, ObjectMetadata> objectAndMetadata = getS3ObjectAndMetadata(bucket, key, ssecLocal);
            int sizeOfFile = (int) objectAndMetadata.getValue().getContentLength();
            fieldCount = sizeOfFile / sizeArray;
            totalSize = sizeOfFile;
            objectAndMetadata.getKey().close();
        } catch (Exception e) {
            System.err.println("Not possible to get the object :" + key);
            e.printStackTrace();
            return Status.ERROR;
        }
    }
    byte[] destinationArray = new byte[totalSize];
    int offset = 0;
    for (int i = 0; i < fieldCount; i++) {
        System.arraycopy(sourceArray, 0, destinationArray, offset, sizeArray);
        offset += sizeArray;
    }
    try (InputStream input = new ByteArrayInputStream(destinationArray)) {
        ObjectMetadata metadata = new ObjectMetadata();
        metadata.setContentLength(totalSize);
        PutObjectRequest putObjectRequest = null;
        if (sseLocal.equals("true")) {
            metadata.setSSEAlgorithm(ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION);
            putObjectRequest = new PutObjectRequest(bucket, key, input, metadata);
        } else if (ssecLocal != null) {
            putObjectRequest = new PutObjectRequest(bucket, key, input, metadata).withSSECustomerKey(ssecLocal);
        } else {
            putObjectRequest = new PutObjectRequest(bucket, key, input, metadata);
        }
        try {
            PutObjectResult res = s3Client.putObject(putObjectRequest);
            if (res.getETag() == null) {
                return Status.ERROR;
            } else {
                if (sseLocal.equals("true")) {
                    System.out.println("Uploaded object encryption status is " + res.getSSEAlgorithm());
                } else if (ssecLocal != null) {
                    System.out.println("Uploaded object encryption status is " + res.getSSEAlgorithm());
                }
            }
        } catch (Exception e) {
            System.err.println("Not possible to write object :" + key);
            e.printStackTrace();
            return Status.ERROR;
        }
    } catch (Exception e) {
        System.err.println("Error in the creation of the stream :" + e.toString());
        e.printStackTrace();
        return Status.ERROR;
    }
    return Status.OK;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) PutObjectResult(com.amazonaws.services.s3.model.PutObjectResult) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) S3Object(com.amazonaws.services.s3.model.S3Object) S3Object(com.amazonaws.services.s3.model.S3Object) HashMap(java.util.HashMap) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) DBException(com.yahoo.ycsb.DBException) PutObjectRequest(com.amazonaws.services.s3.model.PutObjectRequest)

Example 88 with PutObjectRequest

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

the class S3ComponentExistingBucketTest method sendCustomHeaderValues.

@Test
public void sendCustomHeaderValues() throws Exception {
    result.expectedMessageCount(1);
    final Date now = new Date();
    final Map<String, String> userMetadata = new HashMap<String, String>();
    userMetadata.put("CamelName", "Camel");
    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.USER_METADATA, userMetadata);
            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("mycamelbucket", 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(userMetadata, putObjectRequest.getMetadata().getUserMetadata());
    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 89 with PutObjectRequest

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

the class S3ComponentExistingBucketTest method sendInOut.

@Test
public void sendInOut() throws Exception {
    result.expectedMessageCount(1);
    Exchange exchange = template.send("direct:start", ExchangePattern.InOut, 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.getOut());
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) PutObjectRequest(com.amazonaws.services.s3.model.PutObjectRequest) Test(org.junit.Test)

Example 90 with PutObjectRequest

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

the class S3ComponentFileTest method sendFile.

@Test
public void sendFile() throws Exception {
    result.expectedMessageCount(1);
    Exchange exchange = templateKeep.send("direct:startKeep", 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());
    assertFileExists(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)18 BusinessObjectDataKey (org.finra.herd.model.api.xml.BusinessObjectDataKey)14 StorageUnitEntity (org.finra.herd.model.jpa.StorageUnitEntity)14 PutObjectRequest (software.amazon.awssdk.services.s3.model.PutObjectRequest)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