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);
}
}
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;
}
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());
}
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());
}
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());
}
Aggregations