我正在向 S3 存储桶添加资源策略文档。
当我创建一个新的存储桶时它工作正常:
const newbucket = new s3.Bucket(this, 'newBucket', {
websiteIndexDocument : 'index.html',
bucketName : 'NewBucket'
});
newbucket.addToResourcePolicy(new iam.PolicyStatement({
effect : iam.Effect.ALLOW,
actions: ['s3:*'],
resources: [newbucket.arnForObjects('*')],
principals: [new iam.AnyPrincipal],
}));
newbucket.addToResourcePolicy(new iam.PolicyStatement({
effect : iam.Effect.DENY,
actions: ['s3:*'],
resources: [newbucket.arnForObjects('*')],
principals: [new iam.AnyPrincipal],
conditions : {
'NotIpAddress' : {
'aws:SourceIp' : '***.***.***.***'
}
}
}));
但是,如果我尝试获取已经存在的存储桶并添加策略文档,则它不起作用:
const existingbucket = Bucket.fromBucketAttributes(this, 'ImportedBucket',{
bucketName :'ExistingBucket'
})
existingbucket.addToResourcePolicy(new iam.PolicyStatement({
effect : iam.Effect.ALLOW,
actions: ['s3:*'],
resources: [existingbucket.arnForObjects('*')],
principals: [new iam.AnyPrincipal],
}));
不会添加资源策略文档。
此外,此代码删除现有的策略文档并将其设为空白。
有人对此问题有经验或解决方案吗?
最佳答案
是的,这是可能的,我使用 python cdk 做到了。这里有工作。 https://github.com/aws/aws-cdk/issues/6548
那里使用了 CfnBucketPolicy。
existing_bucket=s3.Bucket.from_bucket_attributes(self, 'ImportedBucket',
bucket_arn="arn:aws:s3:::bucket"
)
bucket_policy=iam.PolicyStatement(
actions=["s3:Get*", "s3:List*"],
resources=[existing_bucket.arn_for_objects('*')],
principals=[iam.AccountRootPrincipal()]
)
s3.CfnBucketPolicy(self, 'bucketpolicy',
bucket=existing_bucket.bucket_name,
policy_document=iam.PolicyDocument(statements=[bucket_policy])
)
关于javascript - 如何在 JavaScript 中使用 CDK 将资源策略添加到现有 S3 存储桶?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60087302/