Han*_*Kim 7 javascript amazon-s3 aws-cdk
我正在将资源策略文档添加到 S3 存储桶。
当我创建一个新的 Bucket 时它工作正常:
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' : '***.***.***.***'
}
}
}));
Run Code Online (Sandbox Code Playgroud)
但是,如果我尝试获取已经存在的存储桶并添加策略文档,则它不起作用:
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],
}));
Run Code Online (Sandbox Code Playgroud)
不会添加资源策略文档。
此外,此代码会删除现有的策略文档并将其设为空白。
任何人都有关于这个问题的经验或解决方案?
小智 5
是的,这是可能的,我使用 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])
)
Run Code Online (Sandbox Code Playgroud)