Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅
中国的 Amazon Web Services 服务入门
(PDF)。
管理对象标签
本节介绍如何使用适用于 Java 和 .NET 的 Amazon 开发工具包或 Amazon S3 控制台管理对象标签。
对象标签为您提供了对存储进行分类的方法。每个标签都是遵循以下规则的键值对:
有关对象标签的更多信息,请参阅 使用标签对存储进行分类。有关标签限制的更多信息,请参阅《Amazon Billing and Cost Management 用户指南》中的用户定义的标签限制。
向对象添加标签
-
登录到Amazon Web Services Management Console,然后通过以下网址打开 Amazon S3 控制台:https://console.aws.amazon.com/s3/。
-
在左侧导航窗格中,选择存储桶,然后选择通用存储桶选项卡。导航到包含要更改的对象的 Amazon S3 存储桶或文件夹。
-
选中待更改对象的名称左侧的复选框。
-
在操作菜单上选择编辑标签。
-
查看列出的对象,然后选择添加标签。
-
每个对象标签都是一个键值对。输入 Key (键) 和 Value (值)。要添加另一个标签,请选择 Add Tag (添加标签)。
您最多可以为一个对象输入 10 个标签。
-
选择保存更改。
Amazon S3 会将标签添加给指定对象。
有关更多信息,另请参阅本指南中的 在 Amazon S3 控制台中查看对象属性 和 上传对象。
- Java
-
以下示例演示如何使用Amazon SDK for Java为新对象设置标签并检索或替换现有对象的标签。有关对象标签的更多信息,请参阅使用标签对存储进行分类。有关创建和测试有效示例的说明,请参阅《Amazon 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
-
以下示例演示如何使用Amazon SDK for .NET为新对象设置标签并检索或替换现有对象的标签。有关对象标签的更多信息,请参阅使用标签对存储进行分类。
有关设置和运行代码示例的信息,请参阅《适用于 .NET 的 Amazon SDK 开发人员指南》中的适用于 .NET 的 Amazon SDK 入门。
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);
}
}
}
}