aws-doc-sdk-examples icon indicating copy to clipboard operation
aws-doc-sdk-examples copied to clipboard

Tag or meta Information of object ] <<Retrieve meta data for object in s3>>

Open heyitsaakash opened this issue 6 years ago • 4 comments

What are you trying to accomplish?

[Provide a clear and brief description of what the code example should do. For example, "I want to upload a file to the cloud."]

Which AWS services, if known, should this code example use?

s3 * * *

aws sdk java

* * *

we have a facility to store tags or meta info in object in s3 in console. In listobject api we get object summary in response but nothing about meta info or tags. How to get same.

heyitsaakash avatar Jan 08 '20 10:01 heyitsaakash

I will look into this and provide some code on How To read tags

scmacdon avatar Jan 08 '20 17:01 scmacdon

I have developed AWS Java logic to read the tags that belong to an object within an AWS S3 bucket.

Here is the Java 2 version:

package com.example.s3;

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.*;

import java.util.Iterator; import java.util.List;

public class GetObjectTags {

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.java2.list_objects.main]
    String bucketName = args[0];
    String keyName = args[1];

    try {
        Region region = Region.US_WEST_2;
        S3Client s3 = S3Client.builder().region(region).build();


        // create a GetObjectTaggingRequest instance
        GetObjectTaggingRequest  getTaggingRequest = GetObjectTaggingRequest
                .builder()
                .key(keyName)
                .bucket(bucketName)
                .build();

        // get the tags for this AWS S3 object
        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);
    }
}

}

scmacdon avatar Jan 08 '20 20:01 scmacdon

Here is Java Version 1 example:

package aws.example.s3;

import com.amazonaws.AmazonServiceException; import com.amazonaws.regions.Regions; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3ClientBuilder; import com.amazonaws.services.s3.model.*;

import java.io.IOException; import java.util.Iterator; import java.util.List;

public class GetObjectTags {

public static void main(String[] args) throws IOException {


    if (args.length < 2) {
        System.out.println("Please specify a bucket name and key name");
        System.exit(1);
    }

    // snippet-start:[s3.java2.list_objects.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);
    }
}

}

scmacdon avatar Jan 08 '20 20:01 scmacdon

thanks will check this out

heyitsaakash avatar Jan 15 '20 07:01 heyitsaakash