Search in sources :

Example 11 with GetObjectTaggingRequest

use of com.amazonaws.services.s3.model.GetObjectTaggingRequest in project aws-doc-sdk-examples by awsdocs.

the class GetObjectTags2 method main.

public static void main(String[] args) {
    if (args.length < 2) {
        System.out.println("Please specify a bucket name and key name");
        System.exit(1);
    }
    // snippet-start:[s3.java.getobjecttags.main]
    String bucketName = args[0];
    String keyName = args[1];
    System.out.println("Retrieving Object Tags for  " + keyName);
    final AmazonS3 s3 = AmazonS3ClientBuilder.standard().withRegion(Regions.DEFAULT_REGION).build();
    try {
        GetObjectTaggingRequest getTaggingRequest = new GetObjectTaggingRequest(bucketName, keyName);
        GetObjectTaggingResult tags = s3.getObjectTagging(getTaggingRequest);
        List<Tag> tagSet = tags.getTagSet();
        // Iterate through the list
        Iterator<Tag> tagIterator = tagSet.iterator();
        while (tagIterator.hasNext()) {
            Tag tag = (Tag) tagIterator.next();
            System.out.println(tag.getKey());
            System.out.println(tag.getValue());
        }
    } catch (AmazonServiceException e) {
        System.err.println(e.getErrorMessage());
        System.exit(1);
    }
// snippet-end:[s3.java.getobjecttags.main]
}
Also used : GetObjectTaggingRequest(com.amazonaws.services.s3.model.GetObjectTaggingRequest) AmazonS3(com.amazonaws.services.s3.AmazonS3) AmazonServiceException(com.amazonaws.AmazonServiceException) Tag(com.amazonaws.services.s3.model.Tag) GetObjectTaggingResult(com.amazonaws.services.s3.model.GetObjectTaggingResult)

Example 12 with GetObjectTaggingRequest

use of com.amazonaws.services.s3.model.GetObjectTaggingRequest 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 13 with GetObjectTaggingRequest

use of com.amazonaws.services.s3.model.GetObjectTaggingRequest 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 (com.amazonaws.services.s3.model.GetObjectTaggingRequest)8 GetObjectTaggingResult (com.amazonaws.services.s3.model.GetObjectTaggingResult)8 Tag (com.amazonaws.services.s3.model.Tag)8 PutObjectRequest (com.amazonaws.services.s3.model.PutObjectRequest)5 ByteArrayInputStream (java.io.ByteArrayInputStream)5 File (java.io.File)5 ArrayList (java.util.ArrayList)5 S3FileTransferRequestParamsDto (org.finra.herd.model.dto.S3FileTransferRequestParamsDto)5 AmazonServiceException (com.amazonaws.AmazonServiceException)4 Test (org.junit.Test)4 AmazonS3 (com.amazonaws.services.s3.AmazonS3)3 ObjectMetadata (com.amazonaws.services.s3.model.ObjectMetadata)3 ObjectTagging (com.amazonaws.services.s3.model.ObjectTagging)3 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 BusinessObjectDataKey (org.finra.herd.model.api.xml.BusinessObjectDataKey)2 StorageFile (org.finra.herd.model.api.xml.StorageFile)2 PutObjectTaggingRequest (software.amazon.awssdk.services.s3.model.PutObjectTaggingRequest)2