AWS SDK を使用して Amazon S3 オブジェクトのタグの使用を開始する - Amazon Simple Storage Service

AWS SDK を使用して Amazon S3 オブジェクトのタグの使用を開始する

次のコード例は、Amazon S3 オブジェクトのタグの使用を開始する方法を示しています。

.NET
AWS SDK for .NET
注記

GitHub には、その他のリソースもあります。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

using System; using System.Collections.Generic; using System.Threading.Tasks; using Amazon; using Amazon.S3; using Amazon.S3.Model; /// <summary> /// This example shows how to work with tags in Amazon Simple Storage /// Service (Amazon S3) objects. /// </summary> public class ObjectTag { public static async Task Main() { string bucketName = "doc-example-bucket"; string keyName = "newobject.txt"; string filePath = @"*** file path ***"; // Specify your bucket region (an example region is shown). RegionEndpoint bucketRegion = RegionEndpoint.USWest2; var client = new AmazonS3Client(bucketRegion); await PutObjectsWithTagsAsync(client, bucketName, keyName, filePath); } /// <summary> /// This method uploads an object with tags. It then shows the tag /// values, changes the tags, and shows the new tags. /// </summary> /// <param name="client">The Initialized Amazon S3 client object used /// to call the methods to create and change an objects tags.</param> /// <param name="bucketName">A string representing the name of the /// bucket where the object will be stored.</param> /// <param name="keyName">A string representing the key name of the /// object to be tagged.</param> /// <param name="filePath">The directory location and file name of the /// object to be uploaded to the Amazon S3 bucket.</param> public static async Task PutObjectsWithTagsAsync(IAmazonS3 client, string bucketName, string keyName, string filePath) { try { // Create 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); // Now retrieve the new object's tags. GetObjectTaggingRequest getTagsRequest = new GetObjectTaggingRequest() { BucketName = bucketName, Key = keyName, }; GetObjectTaggingResponse objectTags = await client.GetObjectTaggingAsync(getTagsRequest); // Display the tag values. objectTags.Tagging .ForEach(t => Console.WriteLine($"Key: {t.Key}, Value: {t.Value}")); Tagging newTagSet = new Tagging() { 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); // Retrieve the tags again and show the values. GetObjectTaggingRequest getTagsRequest2 = new GetObjectTaggingRequest() { BucketName = bucketName, Key = keyName, }; GetObjectTaggingResponse objectTags2 = await client.GetObjectTaggingAsync(getTagsRequest2); objectTags2.Tagging .ForEach(t => Console.WriteLine($"Key: {t.Key}, Value: {t.Value}")); } catch (AmazonS3Exception ex) { Console.WriteLine( $"Error: '{ex.Message}'"); } } }
  • API の詳細については、「AWS SDK for .NET API リファレンス」の「GetObjectTagging」を参照してください。

AWS SDK デベロッパーガイドとコード例の完全なリストについては、「このサービスを AWS SDK で使用する」を参照してください。このトピックには、使用開始方法に関する情報と、以前の SDK バージョンの詳細も含まれています。