use of software.amazon.awssdk.services.s3.model.GetObjectRequest in project aws-doc-sdk-examples by awsdocs.
the class DisplayFacesFrame method getObjectBytes.
public static byte[] getObjectBytes(S3Client s3, String bucketName, String keyName) {
try {
GetObjectRequest objectRequest = GetObjectRequest.builder().key(keyName).bucket(bucketName).build();
ResponseBytes<GetObjectResponse> objectBytes = s3.getObjectAsBytes(objectRequest);
byte[] data = objectBytes.asByteArray();
return data;
} 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 GetObjectPresignedUrl method getPresignedUrl.
// snippet-start:[presigned.java2.getobjectpresigned.main]
public static void getPresignedUrl(S3Presigner presigner, String bucketName, String keyName) {
try {
GetObjectRequest getObjectRequest = GetObjectRequest.builder().bucket(bucketName).key(keyName).build();
GetObjectPresignRequest getObjectPresignRequest = GetObjectPresignRequest.builder().signatureDuration(Duration.ofMinutes(10)).getObjectRequest(getObjectRequest).build();
// Generate the presigned request
PresignedGetObjectRequest presignedGetObjectRequest = presigner.presignGetObject(getObjectPresignRequest);
// Log the presigned URL
System.out.println("Presigned URL: " + presignedGetObjectRequest.url());
HttpURLConnection connection = (HttpURLConnection) presignedGetObjectRequest.url().openConnection();
presignedGetObjectRequest.httpRequest().headers().forEach((header, values) -> {
values.forEach(value -> {
connection.addRequestProperty(header, value);
});
});
// Send any request payload that the service needs (not needed when isBrowserExecutable is true)
if (presignedGetObjectRequest.signedPayload().isPresent()) {
connection.setDoOutput(true);
try (InputStream signedPayload = presignedGetObjectRequest.signedPayload().get().asInputStream();
OutputStream httpOutputStream = connection.getOutputStream()) {
IoUtils.copy(signedPayload, httpOutputStream);
}
}
// Download the result of executing the request
try (InputStream content = connection.getInputStream()) {
System.out.println("Service returned response: ");
IoUtils.copy(content, System.out);
}
} catch (S3Exception e) {
e.getStackTrace();
} catch (IOException e) {
e.getStackTrace();
}
}
use of software.amazon.awssdk.services.s3.model.GetObjectRequest in project aws-doc-sdk-examples by awsdocs.
the class GetObjectData method getObjectBytes.
// snippet-start:[s3.java2.getobjectdata.main]
public static void getObjectBytes(S3Client s3, String bucketName, String keyName, String path) {
try {
GetObjectRequest objectRequest = GetObjectRequest.builder().key(keyName).bucket(bucketName).build();
ResponseBytes<GetObjectResponse> objectBytes = s3.getObjectAsBytes(objectRequest);
byte[] data = objectBytes.asByteArray();
// Write the data to a local file
File myFile = new File(path);
OutputStream os = new FileOutputStream(myFile);
os.write(data);
System.out.println("Successfully obtained bytes from an S3 object");
os.close();
} catch (IOException ex) {
ex.printStackTrace();
} catch (S3Exception e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
}
use of software.amazon.awssdk.services.s3.model.GetObjectRequest in project aws-doc-sdk-examples by awsdocs.
the class KMSEncryptionExample method getEncryptedData.
// Obtain the encrypted data, decrypt it, and write the data to a text file
public static void getEncryptedData(S3Client s3, String bucketName, String objectName, String path, String keyId) {
try {
GetObjectRequest objectRequest = GetObjectRequest.builder().key(objectName).bucket(bucketName).build();
// Get the byte[] from the Amazon S3 bucket
ResponseBytes<GetObjectResponse> objectBytes = s3.getObjectAsBytes(objectRequest);
byte[] data = objectBytes.asByteArray();
// Decrypt the data by using the AWS Key Management Service
byte[] unEncryptedData = decryptData(data, keyId);
// Write the data to a local file
File myFile = new File(path);
OutputStream os = new FileOutputStream(myFile);
os.write(unEncryptedData);
System.out.println("Successfully obtained and decrypted bytes from the Amazon S3 bucket");
// Close the file
os.close();
} catch (IOException ex) {
ex.printStackTrace();
} catch (S3Exception e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
}
use of software.amazon.awssdk.services.s3.model.GetObjectRequest in project aws-doc-sdk-examples by awsdocs.
the class ExportEndpoints method downloadFromS3.
// Downloads files from an Amazon S3 bucket and writes them to the path location
public static void downloadFromS3(S3Client s3Client, String path, String s3BucketName, List<String> objectKeys) {
try {
for (String key : objectKeys) {
GetObjectRequest objectRequest = GetObjectRequest.builder().bucket(s3BucketName).key(key).build();
ResponseBytes<GetObjectResponse> objectBytes = s3Client.getObjectAsBytes(objectRequest);
byte[] data = objectBytes.asByteArray();
// Write the data to a local file
String fileSuffix = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
path = path + fileSuffix + ".gz";
File myFile = new File(path);
OutputStream os = new FileOutputStream(myFile);
os.write(data);
}
System.out.println("Download finished.");
} catch (S3Exception | NullPointerException | IOException e) {
System.err.println(e.getMessage());
System.exit(1);
}
}
Aggregations