Search in sources :

Example 1 with GetObjectTaggingResponse

use of software.amazon.awssdk.services.s3.model.GetObjectTaggingResponse 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);
    }
}
Also used : GetObjectTaggingRequest(software.amazon.awssdk.services.s3.model.GetObjectTaggingRequest) PutObjectTaggingRequest(software.amazon.awssdk.services.s3.model.PutObjectTaggingRequest) GetObjectTaggingResponse(software.amazon.awssdk.services.s3.model.GetObjectTaggingResponse) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) ArrayList(java.util.ArrayList) Tagging(software.amazon.awssdk.services.s3.model.Tagging) Tag(software.amazon.awssdk.services.s3.model.Tag)

Example 2 with GetObjectTaggingResponse

use of software.amazon.awssdk.services.s3.model.GetObjectTaggingResponse 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);
    }
}
Also used : GetObjectTaggingRequest(software.amazon.awssdk.services.s3.model.GetObjectTaggingRequest) PutObjectTaggingRequest(software.amazon.awssdk.services.s3.model.PutObjectTaggingRequest) GetObjectTaggingResponse(software.amazon.awssdk.services.s3.model.GetObjectTaggingResponse) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) ArrayList(java.util.ArrayList) Tagging(software.amazon.awssdk.services.s3.model.Tagging) Tag(software.amazon.awssdk.services.s3.model.Tag)

Example 3 with GetObjectTaggingResponse

use of software.amazon.awssdk.services.s3.model.GetObjectTaggingResponse in project aws-doc-sdk-examples by awsdocs.

the class GetObjectTags method listTags.

// snippet-start:[s3.java2.getobjecttags.main]
public static void listTags(S3Client s3, String bucketName, String keyName) {
    try {
        GetObjectTaggingRequest getTaggingRequest = GetObjectTaggingRequest.builder().key(keyName).bucket(bucketName).build();
        GetObjectTaggingResponse tags = s3.getObjectTagging(getTaggingRequest);
        List<Tag> tagSet = tags.tagSet();
        // Write out the tags
        Iterator<Tag> tagIterator = tagSet.iterator();
        while (tagIterator.hasNext()) {
            Tag tag = (Tag) tagIterator.next();
            System.out.println(tag.key());
            System.out.println(tag.value());
        }
    } catch (S3Exception e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
}
Also used : GetObjectTaggingRequest(software.amazon.awssdk.services.s3.model.GetObjectTaggingRequest) GetObjectTaggingResponse(software.amazon.awssdk.services.s3.model.GetObjectTaggingResponse) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) Tag(software.amazon.awssdk.services.s3.model.Tag)

Aggregations

GetObjectTaggingRequest (software.amazon.awssdk.services.s3.model.GetObjectTaggingRequest)3 GetObjectTaggingResponse (software.amazon.awssdk.services.s3.model.GetObjectTaggingResponse)3 S3Exception (software.amazon.awssdk.services.s3.model.S3Exception)3 Tag (software.amazon.awssdk.services.s3.model.Tag)3 ArrayList (java.util.ArrayList)2 PutObjectTaggingRequest (software.amazon.awssdk.services.s3.model.PutObjectTaggingRequest)2 Tagging (software.amazon.awssdk.services.s3.model.Tagging)2