객체 태그 관리
이 섹션에서는 AWS SDK for Java 및 .NET 또는 Amazon S3 콘솔을 사용하여 객체 태그를 관리하는 방법에 대해 설명합니다.
객체 태그 지정을 통해 범용 버킷에서 스토리지를 분류할 수 있습니다. 모든 태그는 다음 규칙이 적용되는 키-값 페어입니다.
-
한 객체에 태그를 최대 10개까지 연결할 수 있습니다. 각 객체에 연결된 태그에는 고유한 태그 키가 있어야 합니다.
-
태그 키의 최대 길이는 128개 유니코드 문자이며, 태그 값의 최대 길이는 256개 유니코드 문자입니다. Amazon S3 객체 태그는 내부적으로 UTF-16 형식으로 표시됩니다. 참고로 UTF-16에서 문자는 1 또는 2자 위치를 차지합니다.
-
키와 값은 대/소문자를 구분합니다.
객체 태그에 대한 자세한 내용은 태그를 사용하여 스토리지 분류 섹션을 참조하세요. 태그 제한에 대한 자세한 내용은 AWS 결제 및 비용 관리 사용 설명서의 사용자 정의 태그 제한을 참조하세요.
객체에 태그 추가
-
AWS Management Console에 로그인한 후 https://console.aws.amazon.com/s3/에서 S3 콘솔을 엽니다.
-
왼쪽 탐색 창에서 범용 버킷을 선택합니다.
-
버킷 목록에서 객체가 포함된 버킷의 이름을 선택합니다.
-
변경할 객체의 이름 왼쪽에 있는 확인란을 선택합니다.
-
작업(Actions) 메뉴에서 태그 편집(Edit tags)을 선택합니다.
-
나열된 객체를 검토하고 태그 추가(Add tags)를 선택합니다.
-
각 객체 태그는 키-값 페어입니다. 키와 값을 입력합니다. 태그를 더 추가하려면 태그 추가(Add Tag)를 선택합니다.
한 개체에 태그를 최대 10개까지 입력할 수 있습니다.
-
[변경 사항 저장(Save changes)]을 선택합니다.
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를 사용하여 새 객체에 대한 태그를 설정하고 기존 객체에 대한 태그를 가져오거나 대체하는 방법을 보여줍니다. 객체 태그 지정에 대한 자세한 정보는 태그를 사용하여 스토리지 분류 단원을 참조하십시오.
코드 예제 설정 및 실행에 대한 자세한 내용은 AWS SDK for .NET 개발자 안내서의 AWS SDK for .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);
}
}
}
}