简体   繁体   English

AWS S3 存储桶标记

[英]AWS S3 bucket tagging

I know how to place tags on S3 object, for example(based on AWS Java SDK):我知道如何在 S3 对象上放置标签,例如(基于 AWS Java SDK):

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);

but I need to associate some meta information with AWS S3 Bucket, so I wondering if I can place tags to AWS S3 Bucket itself?但是我需要将一些元信息与 AWS S3 Bucket 相关联,所以我想知道我是否可以将标签放置到 AWS S3 Bucket 本身? For example I need to keep client Id for each AWS S3 Bucket and then read it in AWS Lamda triggered by S3 event.例如,我需要为每个 AWS S3 存储桶保留客户端 ID,然后在由 S3 事件触发的 AWS Lamda 中读取它。 Is it possible to use AWS S3 Bucket tags for this purpose and if so, please show an example.是否可以为此目的使用 AWS S3 Bucket 标签,如果可以,请举例说明。 if no - please suggest a way I can achive it.如果没有 - 请提出一种我可以实现的方法。

You could use the following snippet to retrieve bucket tags:您可以使用以下代码段来检索存储桶标签:

    final AmazonS3 s3 = AmazonS3ClientBuilder.standard().withRegion(Regions.US_EAST_1).build();
    BucketTaggingConfiguration bucketTaggingConfiguration = s3.getBucketTaggingConfiguration("dev-udp-data");
    System.out.println(bucketTaggingConfiguration.getTagSet());

You could use following snippets to give tags to the bucket :您可以使用以下代码段为存储桶提供标签:

if you are creating a new bucket :如果您要创建新存储桶:

s3_resource = boto3.resource('s3')
bucket_tagging = s3_resource.BucketTagging("bucket_name")
    response = bucket_tagging.put(
        Tagging = {
            'TagSet' : tag_set    //your json format tag_set
    })

if the bucket already exists then first get the tags and append your new tags :如果存储桶已存在,则首先获取标签并附加您的新标签:

s3 = boto3.resource('s3')
bucket_tagging = s3.BucketTagging("bucket_name")
tags = bucket_tagging.tag_set
tags.append({'Key': 'bucket_key', 'Value': owner})
Set_Tag = bucket_tagging.put(Tagging={'TagSet':tags})

Hope it helps !希望能帮助到你 !

If don't already have a client:如果还没有客户:

static final AmazonS3 s3 = AmazonS3ClientBuilder.standard()
        .withRegion(Regions.US_EAST_1)
        .withCredentials(new ProfileCredentialsProvider("<your-profile-name>"))
        .build();

Then you have to get the tag list, if it exists, and append the new tags or create a new tagging configuration.然后您必须获取标签列表(如果存在),并附加新标签或创建新的标签配置。

BucketTaggingConfiguration bucketTaggingConfiguration = amazonS3.getBucketTaggingConfiguration(resourceId);
if (null != bucketTaggingConfiguration) {
    bucketTaggingConfiguration.getAllTagSets().get(0).setTag("tagKey", "tagValue");
}
else {
    TagSet tagSet = new TagSet();
    tagSet.setTag("tagKey", "tagValue");

    List<TagSet> tagSetList = new ArrayList<>();
    tagSetList.add(tagSet);

    bucketTaggingConfiguration = new BucketTaggingConfiguration();
    bucketTaggingConfiguration.setTagSets(tagSetList);
}
amazonS3.setBucketTaggingConfiguration(new SetBucketTaggingConfigurationRequest(resourceId, bucketTaggingConfiguration));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM