use of com.amazonaws.s3.model.GetObjectRequest in project trellis-extensions by trellis-ldp.
the class S3MementoServiceTest method testResourceError.
@Test
void testResourceError() {
final AmazonS3 mockClient = mock(AmazonS3.class);
final ObjectMetadata mockMetadata = mock(ObjectMetadata.class);
final GetObjectRequest mockRequest = mock(GetObjectRequest.class);
final S3Object mockObject = mock(S3Object.class);
when(mockClient.getObject(eq(mockRequest))).thenReturn(mockObject);
when(mockObject.getObjectContent()).thenAnswer(inv -> {
throw new IOException("Expected");
});
final Resource testResource = new S3Resource(mockMetadata, mockClient, mockRequest, "");
assertThrows(TrellisRuntimeException.class, testResource::stream);
}
use of com.amazonaws.s3.model.GetObjectRequest in project nrtsearch by Yelp.
the class ContentDownloaderImpl method getVersionContent.
@Override
public void getVersionContent(final String serviceName, final String resource, final String hash, final Path destDirectory) throws IOException {
final String absoluteResourcePath = String.format("%s/%s/%s", serviceName, resource, hash);
final Path parentDirectory = destDirectory.getParent();
final Path tmpFile = parentDirectory.resolve(getTmpName());
final InputStream s3InputStream;
if (downloadAsStream) {
// Stream the file download from s3 instead of writing to a file first
GetObjectMetadataRequest metadataRequest = new GetObjectMetadataRequest(bucketName, absoluteResourcePath);
ObjectMetadata fullMetadata = transferManager.getAmazonS3Client().getObjectMetadata(metadataRequest);
logger.debug("Full object size: " + fullMetadata.getContentLength());
// get metadata for the 1st file part, needed to find the total number of parts
ObjectMetadata partMetadata = transferManager.getAmazonS3Client().getObjectMetadata(metadataRequest.withPartNumber(1));
int numParts = partMetadata.getPartCount() != null ? partMetadata.getPartCount() : 1;
logger.debug("Object parts: " + numParts);
s3InputStream = getObjectStream(absoluteResourcePath, numParts);
logger.debug("Object streaming started...");
} else {
Download download = transferManager.download(new GetObjectRequest(bucketName, absoluteResourcePath), tmpFile.toFile(), new ContentDownloaderImpl.S3ProgressListenerImpl(serviceName, resource, "download"));
try {
download.waitForCompletion();
logger.debug("S3 Download complete");
} catch (InterruptedException e) {
throw new IOException("S3 Download failed", e);
}
s3InputStream = new FileInputStream(tmpFile.toFile());
}
wrapInputStream(destDirectory, parentDirectory, tmpFile, s3InputStream, hash);
}
use of com.amazonaws.s3.model.GetObjectRequest in project nrmn-application by aodn.
the class StagedJobController method downloadFile.
@GetMapping("/job/download/{jobId}")
public void downloadFile(final HttpServletResponse response, @PathVariable String jobId) {
String s3Key = String.format(s3KeyTemplate, jobId);
logger.info(LogInfo.withContext(String.format("Downloading original sheet for job %s from %s%s", jobId, bucketName, s3Key)));
try {
GetObjectRequest objectRequest = GetObjectRequest.builder().key(s3Key).bucket(bucketName).build();
ResponseBytes<GetObjectResponse> objectBytes = s3client.getClient().getObjectAsBytes(objectRequest);
response.getOutputStream().write(objectBytes.asByteArray());
response.flushBuffer();
logger.info(LogInfo.withContext(String.format("Retrieved sheet for job %s from %s/%s", jobId, bucketName, s3Key)));
} catch (IOException ioException) {
logger.error(LogInfo.withContext(String.format("Could not download sheet for job %s from \"%s/%s\".\n%s", jobId, bucketName, s3Key, ioException.getMessage())));
} catch (NoSuchKeyException keyException) {
logger.error(LogInfo.withContext(String.format("key \"%s\" does not exist in bucket \"%s\". Could not download original file for job %s", s3Key, bucketName, jobId)));
} catch (SdkClientException clientException) {
logger.error(LogInfo.withContext(String.format("Could not download original file for job %s. %s", jobId, clientException.getMessage())));
}
}
use of com.amazonaws.s3.model.GetObjectRequest in project cerberus by Nike-Inc.
the class ConfigService method getCipherText.
private String getCipherText(String path) {
final GetObjectRequest request = new GetObjectRequest(bucketName, path);
try {
S3Object s3Object = s3Client.getObject(request);
InputStream object = s3Object.getObjectContent();
return IOUtils.toString(object);
} catch (AmazonServiceException ase) {
if (StringUtils.equalsIgnoreCase(ase.getErrorCode(), "NoSuchKey")) {
final String errorMessage = String.format("The S3 object doesn't exist. Bucket: %s, Key: %s", bucketName, request.getKey());
logger.debug(errorMessage);
throw new IllegalStateException(errorMessage);
} else {
logger.error("Unexpected error communicating with AWS.", ase);
throw ase;
}
} catch (IOException e) {
String errorMessage = String.format("Unable to read contents of S3 object. Bucket: %s, Key: %s, Expected Encoding: %s", bucketName, request.getKey(), Charset.defaultCharset());
logger.error(errorMessage);
throw new IllegalStateException(errorMessage, e);
}
}
use of com.amazonaws.s3.model.GetObjectRequest in project baremaps by baremaps.
the class S3BlobStore method get.
/**
* {@inheritDoc}
*/
@Override
public Blob get(URI uri) throws BlobStoreException {
try {
GetObjectRequest request = GetObjectRequest.builder().bucket(uri.getHost()).key(uri.getPath().substring(1)).build();
ResponseInputStream<GetObjectResponse> responseInputStream = client.getObject(request);
GetObjectResponse getObjectResponse = responseInputStream.response();
return Blob.builder().withContentLength(getObjectResponse.contentLength()).withContentType(getObjectResponse.contentType()).withContentEncoding(getObjectResponse.contentEncoding()).withInputStream(responseInputStream).build();
} catch (S3Exception e) {
throw new BlobStoreException(e);
}
}
Aggregations