use of software.amazon.awssdk.services.s3.model.PutObjectTaggingRequest 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.model.PutObjectTaggingRequest in project aws-doc-sdk-examples by awsdocs.
the class S3Service method tagExistingObject.
// This method tags an existing object.
private void tagExistingObject(S3Client s3, String bucketName, String key, String label, String LabelValue) {
try {
// First need to get existing tag set; otherwise the existing tags are overwritten.
GetObjectTaggingRequest getObjectTaggingRequest = GetObjectTaggingRequest.builder().bucket(bucketName).key(key).build();
GetObjectTaggingResponse response = s3.getObjectTagging(getObjectTaggingRequest);
// Get the existing immutable list - cannot modify this list.
List<Tag> existingList = response.tagSet();
ArrayList<Tag> newTagList = new ArrayList(new ArrayList<>(existingList));
// Create a new tag.
Tag myTag = Tag.builder().key(label).value(LabelValue).build();
// push new tag to list.
newTagList.add(myTag);
Tagging tagging = Tagging.builder().tagSet(newTagList).build();
PutObjectTaggingRequest taggingRequest = PutObjectTaggingRequest.builder().key(key).bucket(bucketName).tagging(tagging).build();
s3.putObjectTagging(taggingRequest);
System.out.println(key + " was tagged with " + label);
} catch (S3Exception e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
}
Aggregations