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 버전에 대한 세부 정보도 포함되어 있습니다.