我在带有TAG的存储桶下手动标记了文件temp1.zip
:key = VirusScan value = succeed
。我想从代码中验证文件上的标签确实为key = VirusScan value = succeed
。
AmazonS3 s3 = null;
s3 = (AmazonS3)((AmazonS3ClientBuilder)((AmazonS3ClientBuilder)AmazonS3ClientBuilder
.standard()
.withCredentials(new ProfileCredentialsProvider()))
.withRegion(Regions.US_EAST_1))
.build();
GetObjectTaggingRequest getTaggingRequest = new GetObjectTaggingRequest(bucketName, keyName);
s3.getObjectTagging(getTaggingRequest);
我在以下方面遇到例外:
s3.getObjectTagging(getTaggingRequest);
指定的键不存在。
此代码很好用:
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);
}
}
}