use of com.amazonaws.s3.model.PutObjectRequest in project camel-kafka-connector by apache.
the class S3Utils method sendFilesFromPath.
public static void sendFilesFromPath(S3Client s3Client, String bucketName, File[] files) {
LOG.debug("Putting S3 objects");
for (File file : files) {
LOG.debug("Trying to read file {}", file.getName());
PutObjectRequest putObjectRequest = PutObjectRequest.builder().bucket(bucketName).key(file.getName()).build();
s3Client.putObject(putObjectRequest, file.toPath());
}
}
use of com.amazonaws.s3.model.PutObjectRequest in project BridgeServer2 by Sage-Bionetworks.
the class StudyConsentService method writeBytesToPublicS3.
/**
* Write the byte array to a bucket at S3. The bucket will be given world read privileges, and the request
* will be returned with the appropriate content type header for the document's MimeType.
*/
void writeBytesToPublicS3(@Nonnull String bucket, @Nonnull String key, @Nonnull byte[] data, @Nonnull MimeType type) throws IOException {
try (InputStream dataInputStream = new ByteArrayInputStream(data)) {
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentType(type.toString());
PutObjectRequest request = new PutObjectRequest(bucket, key, dataInputStream, metadata).withCannedAcl(CannedAccessControlList.PublicRead);
Stopwatch stopwatch = Stopwatch.createStarted();
s3Client.putObject(request);
logger.info("Finished writing to bucket " + bucket + " key " + key + " (" + data.length + " bytes) in " + stopwatch.elapsed(TimeUnit.MILLISECONDS) + " ms");
}
}
use of com.amazonaws.s3.model.PutObjectRequest in project BridgeServer2 by Sage-Bionetworks.
the class StudyConsentServiceTest method publishConsent.
@Test
public void publishConsent() throws Exception {
// This is the document with the footer, where all the template variables have been removed
String transformedDoc = "<doc><p>Document</p><table class=\"bridge-sig-block\"><tbody><tr>" + "<td><div class=\"label\">Name of Adult Participant</div></td><td><img brimg=\"\" " + "alt=\"\" onerror=\"this.style.display='none'\" src=\"cid:consentSignature\" /><div " + "class=\"label\">Signature of Adult Participant</div></td><td><div class=\"label\">" + "Date</div></td></tr><tr><td><div class=\"label\">Email, Phone, or ID</div></td><td>" + "<div class=\"label\">Sharing Option</div></td></tr></tbody></table></doc>";
StudyConsent consent = StudyConsent.create();
consent.setCreatedOn(CREATED_ON);
consent.setSubpopulationGuid(SUBPOP_GUID.getGuid());
when(mockDao.getConsent(SUBPOP_GUID, CREATED_ON)).thenReturn(consent);
when(mockS3Helper.readS3FileAsString(CONSENT_BUCKET, consent.getStoragePath())).thenReturn(DOCUMENT);
App app = App.create();
Subpopulation subpop = Subpopulation.create();
subpop.setGuid(SUBPOP_GUID);
service.setConsentTemplate(new ByteArrayResource("<doc>${consent.body}</doc>".getBytes()));
StudyConsentView result = service.publishConsent(app, subpop, CREATED_ON);
assertEquals(result.getDocumentContent(), DOCUMENT + SIGNATURE_BLOCK);
assertEquals(result.getCreatedOn(), CREATED_ON);
assertEquals(result.getSubpopulationGuid(), SUBPOP_GUID.getGuid());
assertEquals(result.getStudyConsent(), consent);
verify(mockSubpopService).updateSubpopulation(eq(app), subpopCaptor.capture());
assertEquals(subpopCaptor.getValue().getPublishedConsentCreatedOn(), CREATED_ON);
verify(mockS3Client, times(2)).putObject(requestCaptor.capture());
PutObjectRequest request = requestCaptor.getAllValues().get(0);
assertEquals(request.getBucketName(), PUBLICATION_BUCKET);
assertEquals(request.getCannedAcl(), PublicRead);
assertEquals(IOUtils.toString(request.getInputStream(), UTF_8), transformedDoc);
ObjectMetadata metadata = request.getMetadata();
assertEquals(metadata.getContentType(), MimeType.HTML.toString());
request = requestCaptor.getAllValues().get(1);
assertEquals(request.getBucketName(), PUBLICATION_BUCKET);
assertEquals(request.getCannedAcl(), PublicRead);
// The PDF output isn't easily verified...
metadata = request.getMetadata();
assertEquals(metadata.getContentType(), MimeType.PDF.toString());
}
use of com.amazonaws.s3.model.PutObjectRequest in project para by Erudika.
the class AWSFileStore method store.
@Override
public String store(String path, InputStream data) {
if (StringUtils.startsWith(path, "/")) {
path = path.substring(1);
}
if (StringUtils.isBlank(path) || data == null) {
return null;
}
int maxFileSizeMBytes = Para.getConfig().getConfigInt("para.s3.max_filesize_mb", 10);
try {
Map<String, String> om = new HashMap<String, String>(3);
// 180 days
om.put(HttpHeaders.CACHE_CONTROL, "max-age=15552000, must-revalidate");
if (path.endsWith(".gz")) {
om.put(HttpHeaders.CONTENT_ENCODING, "gzip");
path = path.substring(0, path.length() - 3);
}
PutObjectRequest por = PutObjectRequest.builder().bucket(bucket).key(path).metadata(om).acl(ObjectCannedACL.PUBLIC_READ).build();
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
byte[] buf = new byte[1024];
int length;
while ((length = data.read(buf)) > 0) {
baos.write(buf, 0, length);
if (baos.size() > (maxFileSizeMBytes * 1024 * 1024)) {
logger.warn("Failed to store file on S3 because it's too large - {}, {} bytes", path, baos.size());
return null;
}
}
s3.putObject(por, RequestBody.fromBytes(baos.toByteArray()));
}
final String key = path;
return s3.utilities().getUrl(b -> b.bucket(bucket).key(key)).toExternalForm();
} catch (IOException e) {
logger.error(null, e);
} finally {
try {
data.close();
} catch (IOException ex) {
logger.error(null, ex);
}
}
return null;
}
use of com.amazonaws.s3.model.PutObjectRequest in project firehose by odpf.
the class S3Test method shouldThrowException.
@Test
public void shouldThrowException() throws IOException {
S3Config s3Config = ConfigFactory.create(S3Config.class, new HashMap<Object, Object>() {
{
put("S3_TYPE", "SOME_TYPE");
put("SOME_TYPE_S3_BUCKET_NAME", "TestBucket");
put("SOME_TYPE_S3_REGION", "asia");
}
});
S3Client s3Client = Mockito.mock(S3Client.class);
S3 s3Storage = new S3(s3Config, s3Client);
PutObjectRequest putObject = PutObjectRequest.builder().bucket("TestBucket").key("test").build();
byte[] content = "test".getBytes();
SdkClientException exception = SdkClientException.create("test");
Mockito.when(s3Client.putObject(Mockito.any(PutObjectRequest.class), Mockito.any(RequestBody.class))).thenThrow(exception);
BlobStorageException thrown = Assertions.assertThrows(BlobStorageException.class, () -> s3Storage.store("test", content), "BlobStorageException error was expected");
ArgumentCaptor<PutObjectRequest> putObjectRequestArgumentCaptor = ArgumentCaptor.forClass(PutObjectRequest.class);
ArgumentCaptor<RequestBody> requestBodyArgumentCaptor = ArgumentCaptor.forClass(RequestBody.class);
Mockito.verify(s3Client, Mockito.times(1)).putObject(putObjectRequestArgumentCaptor.capture(), requestBodyArgumentCaptor.capture());
Assert.assertEquals(putObject, putObjectRequestArgumentCaptor.getValue());
byte[] expectedBytes = new byte[4];
byte[] actualBytes = new byte[4];
RequestBody.fromBytes(content).contentStreamProvider().newStream().read(expectedBytes);
requestBodyArgumentCaptor.getValue().contentStreamProvider().newStream().read(actualBytes);
Assert.assertEquals(new String(expectedBytes), new String(actualBytes));
Assertions.assertEquals(new BlobStorageException("test", "test", exception), thrown);
}
Aggregations