【问题标题】:AWS S3 bucket taggingAWS S3 存储桶标记
【发布时间】:2019-09-13 07:09:26
【问题描述】:

我知道如何在 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);

但我需要将一些元信息与 AWS S3 Bucket 相关联,所以我想知道是否可以将标签放置到 AWS S3 Bucket 本身?例如,我需要为每个 AWS S3 存储桶保留客户端 ID,然后在由 S3 事件触发的 AWS Lamda 中读取它。是否可以为此目的使用 AWS S3 存储桶标签,如果可以,请举例说明。如果没有 - 请建议我可以实现它的方法。

【问题讨论】:

    标签: amazon-web-services amazon-s3 tags


    【解决方案1】:

    您可以使用以下 sn-p 检索存储桶标签:

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

    【讨论】:

      【解决方案2】:

      您可以使用以下 sn-ps 为存储桶添加标签:

      如果您正在创建一个新存储桶:

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

      如果存储桶已经存在,那么首先获取标签并附加新标签:

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

      希望对你有帮助!

      【讨论】:

        【解决方案3】:

        如果还没有客户:

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

        然后您必须获取标签列表(如果存在),并附加新标签或创建新的标签配置。

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

        【讨论】:

          猜你喜欢
          • 2017-06-26
          • 2018-02-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-01-03
          • 1970-01-01
          • 2014-10-10
          相关资源
          最近更新 更多