管理对象标签 - Amazon Simple Storage Service

管理对象标签

本节介绍如何使用适用于 Java 和 .NET 的 AWS 开发工具包或 Amazon S3 控制台管理对象标签。

对象标签为您提供了对存储进行分类的方法。每个标签都是遵循以下规则的键值对:

  • 您最多可以将 10 个标签与对象关联。与对象关联的标签必须具有唯一的标签键。

  • 标签键的长度最大可以为 128 个 Unicode 字符,标签值的长度最大可以为 256 个 Unicode 字符。Amazon S3 对象标签在内部以 UTF-16 表示。请注意,采用 UTF-16 时,字符占用 1 或 2 个字符位置。

  • 键和值区分大小写。

有关对象标签的更多信息,请参阅 使用标签对存储进行分类。有关标签限制的更多信息,请参阅《AWS Billing and Cost Management 用户指南》中的用户定义的标签限制

向对象添加标签
  1. 登录到AWS Management Console,然后通过以下网址打开 Amazon S3 控制台:https://console.aws.amazon.com/s3/

  2. 在左侧导航窗格中,选择存储桶,然后选择通用存储桶选项卡。导航到包含要更改的对象的 Amazon S3 存储桶或文件夹。

  3. 选中待更改对象的名称左侧的复选框。

  4. 操作菜单上选择编辑标签

  5. 查看列出的对象,然后选择添加标签

  6. 每个对象标签都是一个键值对。输入 Key (键)Value (值)。要添加另一个标签,请选择 Add Tag (添加标签)

    您最多可以为一个对象输入 10 个标签。

  7. 选择保存更改

    Amazon S3 会将标签添加给指定对象。

有关更多信息,另请参阅本指南中的 在 Amazon S3 控制台中查看对象属性上传对象

Java

以下示例演示如何使用AWS SDK for Java为新对象设置标签并检索或替换现有对象的标签。有关对象标签的更多信息,请参阅使用标签对存储进行分类。有关创建和测试有效示例的说明,请参阅《AWS SDK for Java 开发人员指南》中的入门

import com.amazonaws.AmazonServiceException; import com.amazonaws.SdkClientException; import com.amazonaws.auth.profile.ProfileCredentialsProvider; 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.File; import java.util.ArrayList; import java.util.List; public class ManagingObjectTags { public static void main(String[] args) { Regions clientRegion = Regions.DEFAULT_REGION; String bucketName = "*** Bucket name ***"; String keyName = "*** Object key ***"; String filePath = "*** File path ***"; try { AmazonS3 s3Client = AmazonS3ClientBuilder.standard() .withCredentials(new ProfileCredentialsProvider()) .withRegion(clientRegion) .build(); // Create an object, add two new tags, and upload the object to Amazon S3. PutObjectRequest putRequest = new PutObjectRequest(bucketName, keyName, new File(filePath)); List<Tag> tags = new ArrayList<Tag>(); tags.add(new Tag("Tag 1", "This is tag 1")); tags.add(new Tag("Tag 2", "This is tag 2")); putRequest.setTagging(new ObjectTagging(tags)); PutObjectResult putResult = s3Client.putObject(putRequest); // Retrieve the object's tags. GetObjectTaggingRequest getTaggingRequest = new GetObjectTaggingRequest(bucketName, keyName); GetObjectTaggingResult getTagsResult = s3Client.getObjectTagging(getTaggingRequest); // Replace the object's tags with two new tags. List<Tag> newTags = new ArrayList<Tag>(); newTags.add(new Tag("Tag 3", "This is tag 3")); newTags.add(new Tag("Tag 4", "This is tag 4")); s3Client.setObjectTagging(new SetObjectTaggingRequest(bucketName, keyName, new ObjectTagging(newTags))); } catch (AmazonServiceException e) { // The call was transmitted successfully, but Amazon S3 couldn't process // it, so it returned an error response. e.printStackTrace(); } catch (SdkClientException e) { // Amazon S3 couldn't be contacted for a response, or the client // couldn't parse the response from Amazon S3. e.printStackTrace(); } } }
.NET

以下示例演示如何使用AWS SDK for .NET为新对象设置标签并检索或替换现有对象的标签。有关对象标签的更多信息,请参阅使用标签对存储进行分类

有关设置和运行代码示例的信息,请参阅《适用于 .NET 的 AWS SDK 开发人员指南》中的适用于 .NET 的 AWS SDK 入门

using Amazon; using Amazon.S3; using Amazon.S3.Model; using System; using System.Collections.Generic; using System.Threading.Tasks; namespace Amazon.DocSamples.S3 { public class ObjectTagsTest { private const string bucketName = "*** bucket name ***"; private const string keyName = "*** key name for the new object ***"; private const string filePath = @"*** file path ***"; // Specify your bucket region (an example region is shown). private static readonly RegionEndpoint bucketRegion = RegionEndpoint.USWest2; private static IAmazonS3 client; public static void Main() { client = new AmazonS3Client(bucketRegion); PutObjectWithTagsTestAsync().Wait(); } static async Task PutObjectWithTagsTestAsync() { try { // 1. Put an object with tags. var putRequest = new PutObjectRequest { BucketName = bucketName, Key = keyName, FilePath = filePath, TagSet = new List<Tag>{ new Tag { Key = "Keyx1", Value = "Value1"}, new Tag { Key = "Keyx2", Value = "Value2" } } }; PutObjectResponse response = await client.PutObjectAsync(putRequest); // 2. Retrieve the object's tags. GetObjectTaggingRequest getTagsRequest = new GetObjectTaggingRequest { BucketName = bucketName, Key = keyName }; GetObjectTaggingResponse objectTags = await client.GetObjectTaggingAsync(getTagsRequest); for (int i = 0; i < objectTags.Tagging.Count; i++) Console.WriteLine("Key: {0}, Value: {1}", objectTags.Tagging[i].Key, objectTags.Tagging[i].Value); // 3. Replace the tagset. Tagging newTagSet = new Tagging(); newTagSet.TagSet = new List<Tag>{ new Tag { Key = "Key3", Value = "Value3"}, new Tag { Key = "Key4", Value = "Value4" } }; PutObjectTaggingRequest putObjTagsRequest = new PutObjectTaggingRequest() { BucketName = bucketName, Key = keyName, Tagging = newTagSet }; PutObjectTaggingResponse response2 = await client.PutObjectTaggingAsync(putObjTagsRequest); // 4. Retrieve the object's tags. GetObjectTaggingRequest getTagsRequest2 = new GetObjectTaggingRequest(); getTagsRequest2.BucketName = bucketName; getTagsRequest2.Key = keyName; GetObjectTaggingResponse objectTags2 = await client.GetObjectTaggingAsync(getTagsRequest2); for (int i = 0; i < objectTags2.Tagging.Count; i++) Console.WriteLine("Key: {0}, Value: {1}", objectTags2.Tagging[i].Key, objectTags2.Tagging[i].Value); } catch (AmazonS3Exception e) { Console.WriteLine( "Error encountered ***. Message:'{0}' when writing an object" , e.Message); } catch (Exception e) { Console.WriteLine( "Encountered an error. Message:'{0}' when writing an object" , e.Message); } } } }