オブジェクトタグの管理 - Amazon Simple Storage Service

オブジェクトタグの管理

このセクションでは、AWS SDK for Java および .NET、または Amazon S3 コンソールを使用してオブジェクトタグを管理する方法について説明します。

オブジェクトのタグ付けにより、ストレージを分類する方法が提供されます。各タグは、以下のルールに準拠したキーと値のペアです。

  • 1 つのオブジェクトに最大 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. [バケット] リストで、タグを追加するオブジェクトが含まれるバケットの名前を選択します。

    必要に応じてフォルダに移動することもできます。

  3. [ オブジェクト ] リストで、タグを追加するオブジェクトの名前の横にあるチェックボックスをオンにします。

  4. [Actions] メニューで、[Edit tags] を選択します。

  5. リストされたオブジェクトを確認し、[タグの追加] を選択します。

  6. 各オブジェクトタグはキーと値のペアです。キーを入力します。別のタグを追加するには、[タグの追加] を選択します。

    オブジェクトには最大 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); } } } }