Amazon Simple Storage Service
开发人员指南 (API Version 2006-03-01)
AWS 服务或AWS文档中描述的功能,可能因地区/位置而异。点 击 Getting Started with Amazon AWS to see specific differences applicable to the China (Beijing) Region.

使用 SDK (适用于 Java 的 AWS 开发工具包) 管理标签

下面的 Java 代码示例将执行以下操作:

  • 创建带标签的对象。

  • 检索标签集。

  • 更新标签集 (替换现有标签集)。

有关如何创建和测试有效示例的说明,请参阅 测试 Java 代码示例

package; import; import java.util.ArrayList; import java.util.List; import com.amazonaws.auth.BasicAWSCredentials; import; import*; public class ObjectTaggingTest { static String bucketName = "***bucket***"; static String keyName = "***object key name***"; static String filePath = "***filepath***"; public static void main(String[] args) { AmazonS3Client s3client = new AmazonS3Client(new BasicAWSCredentials("<AccessKey>", "<SecretKey>")); // 1. Put object with tags. PutObjectRequest putRequest = new PutObjectRequest(bucketName, keyName, new File(filePath)); List<Tag> tags = new ArrayList<Tag>(); tags.add(new Tag("Key1", "Value1")); tags.add(new Tag("Key2", "Value2")); putRequest.setTagging(new ObjectTagging(tags)); PutObjectResult putResult = s3client.putObject(putRequest); // 2. Retrieve object tags. GetObjectTaggingRequest getTaggingRequest = new GetObjectTaggingRequest(bucketName, keyName); GetObjectTaggingResult getTagsResult = s3client.getObjectTagging(getTaggingRequest); // 3. Replace the tagset. List<Tag> newTags = new ArrayList<Tag>(); newTags.add(new Tag("Key3", "Value3")); newTags.add(new Tag("Key4", "Value4")); s3client.setObjectTagging(new SetObjectTaggingRequest(bucketName, keyName, new ObjectTagging(newTags))); // 4. Retrieve object tags. GetObjectTaggingRequest getTaggingRequest2 = new GetObjectTaggingRequest(bucketName, keyName); GetObjectTaggingResult getTagsResult2 = s3client.getObjectTagging(getTaggingRequest); } }