gpt4 book ai didi

java - 如何对多个对象设置S3存储桶策略?

转载 作者:太空宇宙 更新时间:2023-11-04 09:56:28 25 4
gpt4 key购买 nike

如何对多个对象设置S3存储桶策略?

Statement allowRestrictedWriteStatement = new Statement(Effect.Allow)
.withPrincipals(new Principal("12345678910"))
.withActions(S3Actions.DeleteObject)
.withResources(new S3ObjectResource(myBucketName, "folder1/folder2"));

Statement allowRestrictedWriteStatement2 = new Statement(Effect.Deny)
.withPrincipals(new Principal("12345678910"))
.withActions(S3Actions.DeleteObject)
.withResources(new S3ObjectResource(myBucketName, "folder1/folder3"));

Policy policy = new Policy()
.withStatements(allowRestrictedWriteStatement,allowRestrictedWriteStatement2);

AWSCredentials credentials = new BasicAWSCredentials("xxxxxxxxxxxxx", "yyyyyyyyyyy");
AmazonS3 s3client = new AmazonS3Client(credentials);
s3client.setBucketPolicy(myBucketName,policy.toJson());

借助上述代码,我可以设置两个存储桶对象的允许和拒绝权限。如果我们需要对两个列表上可用的对象设置允许和拒绝,即 listAllow 和 listDeny 各包含 5 个对象,并且我需要对列表的这些对象设置存储桶策略,该怎么办?有什么建议吗?

最佳答案

我找到了解决方案 -

List<Statement> newlistAllow = new ArrayList <>();
for (CommonPrefix objectToDelete:listAllow) {
Statement allowRestrictedWriteStatement = new Statement(Statement.Effect.Allow)
.withPrincipals(new Principal("12345678910"))
.withActions(S3Actions.DeleteObject)
.withResources(new S3ObjectResource(bucketNameToUse,objectToDelete.toString()));
newlistAllow.add(allowRestrictedWriteStatement);
}

List<Statement> newlistDeny = new ArrayList <>();
for (CommonPrefix objectToRetain:listDeny) {
Statement denyRestrictedWriteStatement = new Statement(Statement.Effect.Deny)
.withPrincipals(new Principal("12345678910"))
.withActions(S3Actions.DeleteObject)
.withResources(new S3ObjectResource(bucketNameToUse,objectToRetain.toString()));
newlistDeny.add(denyRestrictedWriteStatement);
}

Policy policyDeny = new Policy();
policyDeny.setStatements(newlistDeny);
Policy policyAllow = new Policy();
policyAllow.setStatements(newlistAllow);

AWSCredentials credentials = new BasicAWSCredentials("xxxxxxxxxxxxxxxxx", "yyyyyyyyyyyyyyyyyyyy");
AmazonS3 s3client = new AmazonS3Client(credentials);

s3client.setBucketPolicy(bucketNameToUse,policyAllow.toJson());
s3client.setBucketPolicy(bucketNameToUse,policyDeny.toJson());

关于java - 如何对多个对象设置S3存储桶策略?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54093469/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com