管理物件標籤 - 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,並開啟位於 https://console.aws.amazon.com/s3/ 的 Amazon S3 主控台。

  2. Buckets (儲存貯體) 清單中,選擇儲存貯體的名稱,其中包含您要新增標籤的物件。

    您也可以選擇性地導覽至資料夾。

  3. Objects (物件) 清單中,選取要新增標籤的物件名稱旁的核取方塊。

  4. Actions (動作) 選單上,選擇 Edit tags (編輯標籤)

  5. 檢閱列出的物件,然後選擇 Add tags (新增標籤)

  6. 每個物件標籤都是一個鍵值對。輸入 Key (索引鍵)Value (數值)。若要新增其他標籤,選擇 Add Tag (新增標籤)

    一個物件最多可以輸入 10 個標籤。

  7. 選擇 Save changes (儲存變更)。

    Amazon S3 會將標籤新增至指定的物件。

如需詳細資訊,請參閱本指南中的 在 Amazon S3 主控台中檢視物件屬性上傳物件

Java

以下範例說明如何使用 AWS SDK for Java 設定新物件的標籤,擷取或取代現存物件的標籤。如需更多物件標記的詳細資訊,請參閱「使用標籤分類儲存空間」。如需建立及測試可行範例的說明,請參閱測試 Amazon S3 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 設定新物件的標籤,擷取或取代現存物件的標籤。如需更多物件標記的詳細資訊,請參閱「使用標籤分類儲存空間」。

如需如何建立及測試工作範例的說明,請參閱「執行 Amazon S3 .NET 程式碼範例」。

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); } } } }