AWS S3存储桶标记

时间:2019-09-13 07:09:26

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

例如,我知道如何在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存储桶标签,如果可以,请显示示例。如果没有,请提出一种实现目标的方法。

3 个答案:

答案 0 :(得分:1)

您可以使用以下代码段检索存储桶标签:

const loginEpic: Types.RootEpic = (action$) =>
    action$.pipe(
        filter(isActionOf(login.request)),
        switchMap(({ payload }) =>
            race(
                from(membershipService.login(payload.username, payload.password)).pipe(
                    mergeMap((token) =>
                        from(membershipService.getUser()).pipe(
                            mergeMap((user) => [syncUser.success(user), login.success(token)]),
                            catchError(error => {
                                // user fetch failed, tell login that user is responsible
                                return of(login.failure(new BaseError("6010", "User sync failed.")));
                            })
                        )
                    ),
                    catchError(error => of(login.failure(error))),
                ),
                action$.pipe(
                    filter(isActionOf(logout.request)),
                    map(() => login.cancel()),
                    take(1)
                )
            )

        )
    )

答案 1 :(得分:1)

如果还没有客户:

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

答案 2 :(得分:0)

您可以使用以下代码片段为存储桶添加标签:

如果要创建新存储桶:

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

希望有帮助!