use of software.amazon.awssdk.services.s3.S3Client 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);
}
}
use of software.amazon.awssdk.services.s3.S3Client in project aws-doc-sdk-examples by awsdocs.
the class CopyObject method copyBucketObject.
// snippet-start:[s3.java2.copy_object.main]
public static String copyBucketObject(S3Client s3, String fromBucket, String objectKey, String toBucket) {
String encodedUrl = null;
try {
encodedUrl = URLEncoder.encode(fromBucket + "/" + objectKey, StandardCharsets.UTF_8.toString());
} catch (UnsupportedEncodingException e) {
System.out.println("URL could not be encoded: " + e.getMessage());
}
CopyObjectRequest copyReq = CopyObjectRequest.builder().copySource(encodedUrl).destinationBucket(toBucket).destinationKey(objectKey).build();
try {
CopyObjectResponse copyRes = s3.copyObject(copyReq);
return copyRes.copyObjectResult().toString();
} catch (S3Exception e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
return "";
}
use of software.amazon.awssdk.services.s3.S3Client in project aws-doc-sdk-examples by awsdocs.
the class CreateBucket method createBucket.
// snippet-start:[s3.java2.create_bucket_waiters.main]
public static void createBucket(S3Client s3Client, String bucketName) {
try {
S3Waiter s3Waiter = s3Client.waiter();
CreateBucketRequest bucketRequest = CreateBucketRequest.builder().bucket(bucketName).build();
s3Client.createBucket(bucketRequest);
HeadBucketRequest bucketRequestWait = HeadBucketRequest.builder().bucket(bucketName).build();
// Wait until the bucket is created and print out the response.
WaiterResponse<HeadBucketResponse> waiterResponse = s3Waiter.waitUntilBucketExists(bucketRequestWait);
waiterResponse.matched().response().ifPresent(System.out::println);
System.out.println(bucketName + " is ready");
} catch (S3Exception e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
}
use of software.amazon.awssdk.services.s3.S3Client in project aws-doc-sdk-examples by awsdocs.
the class DeleteBucketPolicy method deleteS3BucketPolicy.
// snippet-start:[s3.java2.delete_bucket_policy.main]
// Delete the bucket policy
public static void deleteS3BucketPolicy(S3Client s3, String bucketName) {
DeleteBucketPolicyRequest delReq = DeleteBucketPolicyRequest.builder().bucket(bucketName).build();
try {
s3.deleteBucketPolicy(delReq);
System.out.println("Done!");
} catch (S3Exception e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
}
use of software.amazon.awssdk.services.s3.S3Client in project aws-doc-sdk-examples by awsdocs.
the class S3Service method getClient.
private S3Client getClient() {
Region region = Region.US_EAST_1;
S3Client s3 = S3Client.builder().credentialsProvider(EnvironmentVariableCredentialsProvider.create()).region(region).build();
return s3;
}
Aggregations