use of software.amazon.awssdk.services.s3.model.GetObjectRequest in project aws-doc-sdk-examples by awsdocs.
the class S3Service method getObjectBytes.
public byte[] getObjectBytes(String bucketName, String keyName) {
S3Client s3 = getClient();
try {
GetObjectRequest objectRequest = GetObjectRequest.builder().key(keyName).bucket(bucketName).build();
// Return the byte[] from this object.
ResponseBytes<GetObjectResponse> objectBytes = s3.getObjectAsBytes(objectRequest);
return objectBytes.asByteArray();
} catch (S3Exception e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
return null;
}
use of software.amazon.awssdk.services.s3.model.GetObjectRequest in project aws-doc-sdk-examples by awsdocs.
the class S3AsyncStreamOps method main.
public static void main(String[] args) {
final String USAGE = "\n" + "Usage:\n" + " S3AsyncStreamOps <bucketName> <objectKey> <path>\n\n" + "Where:\n" + " bucketName - the name of the Amazon S3 bucket (for example, bucket1). \n\n" + " objectKey - the name of the object (for example, book.pdf). \n" + " path - the local path to the file (for example, C:/AWS/book.pdf). \n";
if (args.length != 3) {
System.out.println(USAGE);
System.exit(1);
}
String bucketName = args[0];
String objectKey = args[1];
String path = args[2];
Region region = Region.US_WEST_2;
S3AsyncClient client = S3AsyncClient.builder().region(region).build();
GetObjectRequest objectRequest = GetObjectRequest.builder().bucket(bucketName).key(objectKey).build();
CompletableFuture<GetObjectResponse> futureGet = client.getObject(objectRequest, AsyncResponseTransformer.toFile(Paths.get(path)));
futureGet.whenComplete((resp, err) -> {
try {
if (resp != null) {
System.out.println("Object downloaded. Details: " + resp);
} else {
err.printStackTrace();
}
} finally {
// Only close the client when you are completely done with it
client.close();
}
});
futureGet.join();
}
use of software.amazon.awssdk.services.s3.model.GetObjectRequest in project gradle by gradle.
the class S3Client method doGetS3Object.
private S3Object doGetS3Object(URI uri, boolean isLightWeight) {
S3RegionalResource s3RegionalResource = new S3RegionalResource(uri);
String bucketName = s3RegionalResource.getBucketName();
String s3BucketKey = s3RegionalResource.getKey();
configureClient(s3RegionalResource);
GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, s3BucketKey);
if (isLightWeight) {
// Skip content download
getObjectRequest.setRange(0, 0);
}
try {
return amazonS3Client.getObject(getObjectRequest);
} catch (AmazonServiceException e) {
String errorCode = e.getErrorCode();
if (null != errorCode && errorCode.equalsIgnoreCase("NoSuchKey")) {
return null;
}
throw ResourceExceptions.getFailed(uri, e);
}
}
use of software.amazon.awssdk.services.s3.model.GetObjectRequest in project alluxio by Alluxio.
the class S3AInputStream method openStream.
/**
* Opens a new stream at mPos if the wrapped stream mIn is null.
*/
private void openStream() throws IOException {
if (mIn != null) {
// stream is already open
return;
}
GetObjectRequest getReq = new GetObjectRequest(mBucketName, mKey);
// If the position is 0, setting range is redundant and causes an error if the file is 0 length
if (mPos > 0) {
getReq.setRange(mPos);
}
AmazonS3Exception lastException = null;
String errorMessage = String.format("Failed to open key: %s bucket: %s, left retry:%d", mKey, mBucketName, mRetryPolicy.getAttemptCount());
while (mRetryPolicy.attempt()) {
try {
mIn = getClient().getObject(getReq).getObjectContent();
return;
} catch (AmazonS3Exception e) {
errorMessage = String.format("Failed to open key: %s bucket: %s attempts: %d error: %s", mKey, mBucketName, mRetryPolicy.getAttemptCount(), e.getMessage());
if (e.getStatusCode() != HttpStatus.SC_NOT_FOUND) {
throw new IOException(errorMessage, e);
}
// Key does not exist
lastException = e;
}
}
// Failed after retrying key does not exist
throw new IOException(errorMessage, lastException);
}
use of software.amazon.awssdk.services.s3.model.GetObjectRequest in project druid by druid-io.
the class StaticS3FirehoseFactory method openObjectStream.
@Override
protected InputStream openObjectStream(URI object, long start) throws IOException {
final String bucket = object.getAuthority();
final String key = S3Utils.extractS3Key(object);
final GetObjectRequest request = new GetObjectRequest(bucket, key);
request.setRange(start);
try {
final S3Object s3Object = s3Client.getObject(request);
if (s3Object == null) {
throw new ISE("Failed to get an s3 object for bucket[%s], key[%s], and start[%d]", bucket, key, start);
}
return s3Object.getObjectContent();
} catch (AmazonS3Exception e) {
throw new IOException(e);
}
}
Aggregations