use of software.amazon.awssdk.services.s3.model.GetObjectResponse in project aws-doc-sdk-examples by awsdocs.
the class MovieLensDatasetProvider method getObjectBytes.
// Checks to see if the dataset is already uploaded to s3.
public static boolean getObjectBytes(S3Client s3Client, String bucketName, String keyName) {
try {
GetObjectRequest objectRequest = GetObjectRequest.builder().key(keyName).bucket(bucketName).build();
ResponseBytes<GetObjectResponse> objectBytes = s3Client.getObjectAsBytes(objectRequest);
byte[] data = objectBytes.asByteArray();
return data.length > 0;
} catch (NoSuchKeyException | NoSuchBucketException ex) {
return false;
} catch (S3Exception s3Exception) {
System.err.println(s3Exception.awsErrorDetails().errorMessage());
System.exit(1);
}
return false;
}
use of software.amazon.awssdk.services.s3.model.GetObjectResponse 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.GetObjectResponse 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();
}
Aggregations