use of software.amazon.awssdk.services.s3.model.S3Exception in project aws-doc-sdk-examples by awsdocs.
the class AbortMultipartUpload method abortUploads.
// snippet-start:[s3.java2.abort_upload.main]
public static void abortUploads(S3Client s3, String bucketName, String accountId) {
try {
ListMultipartUploadsRequest listMultipartUploadsRequest = ListMultipartUploadsRequest.builder().bucket(bucketName).build();
ListMultipartUploadsResponse response = s3.listMultipartUploads(listMultipartUploadsRequest);
List<MultipartUpload> uploads = response.uploads();
AbortMultipartUploadRequest abortMultipartUploadRequest = null;
for (MultipartUpload upload : uploads) {
abortMultipartUploadRequest = AbortMultipartUploadRequest.builder().bucket(bucketName).key(upload.key()).expectedBucketOwner(accountId).uploadId(upload.uploadId()).build();
s3.abortMultipartUpload(abortMultipartUploadRequest);
}
} catch (S3Exception e) {
System.err.println(e.getMessage());
System.exit(1);
}
}
use of software.amazon.awssdk.services.s3.model.S3Exception 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.S3Exception 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.S3Exception in project aws-doc-sdk-examples by awsdocs.
the class KMSEncryptionExample method putEncryptData.
// snippet-start:[s3.java2.kms.main]
// Encrypt data and place the encrypted data into an Amazon S3 bucket
public static void putEncryptData(S3Client s3, String objectName, String bucketName, String objectPath, String keyId) {
try {
PutObjectRequest objectRequest = PutObjectRequest.builder().bucket(bucketName).key(objectName).build();
byte[] myData = getObjectFile(objectPath);
// Encrypt the data by using the AWS Key Management Service
byte[] encryptData = encryptData(keyId, myData);
s3.putObject(objectRequest, RequestBody.fromBytes(encryptData));
} catch (S3Exception e) {
System.err.println(e.getMessage());
System.exit(1);
}
}
use of software.amazon.awssdk.services.s3.model.S3Exception in project aws-doc-sdk-examples by awsdocs.
the class ManagingObjectTags method updateObjectTags.
public static void updateObjectTags(S3Client s3, String bucketName, String objectKey) {
try {
// Retrieve the object's tags.
GetObjectTaggingRequest taggingRequest = GetObjectTaggingRequest.builder().bucket(bucketName).key(objectKey).build();
GetObjectTaggingResponse getTaggingRes = s3.getObjectTagging(taggingRequest);
// Write out the tags.
List<Tag> obTags = getTaggingRes.tagSet();
for (Tag sinTag : obTags) {
System.out.println("The tag key is: " + sinTag.key());
System.out.println("The tag value is: " + sinTag.value());
}
// Replace the object's tags with two new tags.
Tag tag3 = Tag.builder().key("Tag 3").value("This is tag 3").build();
Tag tag4 = Tag.builder().key("Tag 4").value("This is tag 4").build();
List<Tag> tags = new ArrayList<Tag>();
tags.add(tag3);
tags.add(tag4);
Tagging updatedTags = Tagging.builder().tagSet(tags).build();
PutObjectTaggingRequest taggingRequest1 = PutObjectTaggingRequest.builder().bucket(bucketName).key(objectKey).tagging(updatedTags).build();
s3.putObjectTagging(taggingRequest1);
// Write out the modified tags.
GetObjectTaggingResponse getTaggingRes2 = s3.getObjectTagging(taggingRequest);
List<Tag> modTags = getTaggingRes2.tagSet();
for (Tag sinTag : modTags) {
System.out.println("The tag key is: " + sinTag.key());
System.out.println("The tag value is: " + sinTag.value());
}
} catch (S3Exception e) {
System.err.println(e.getMessage());
System.exit(1);
}
}
Aggregations