[英]Create API Gateway that can read non-public S3 bucket using CDK
我正在尝试使用可以访问非公共 S3 存储桶的 CDK 创建代理 API 网关。 为此,我创建了一个角色:
const role = new iam.Role(this, 'apigw-s3-readonly-role', {
roleName: 'ApiGw-S3-ReadOnly',
assumedBy: new iam.ServicePrincipal('apigateway.amazonaws.com'),
})
role.addToPolicy(new iam.PolicyStatement({
resources: [bucket.bucketArn],
actions: ['s3:GetObject']
}))
并像这样创建了一个 API:
const api = new apigw.RestApi(this, 'TestApi')
api.root
.addResource('{file}')
.addMethod('GET', new apigw.AwsIntegration({
service: 's3',
integrationHttpMethod: 'GET',
path: 'my-test-bucket/{file}',
options: {
credentialsRole: role,
requestParameters: {
'integration.request.path.file': 'method.request.path.file'
},
integrationResponses: [{
statusCode: "200"
}]
}
}), {
requestParameters: {
'method.request.path.file': true
}
})
但这需要公开 S3 存储桶。 我认为这是因为它向 S3 发送 http 请求,但我只允许角色中的s3:GetObject
。
那么,如何更改 API 网关以允许访问 S3 存储桶对象而不将其公开。
我尝试了下面的代码,它看起来很有希望,因为它声明了 action GetObject
,但它没有用。
api.root
.addResource('{file}')
.addMethod('GET', new apigw.AwsIntegration({
service: 's3',
action: 'GetObject',
actionParameters: {
Bucket: 'my-test-bucket',
Key: 'file'
},
...
你的保单有问题。 GetObject
权限应应用于 object 而不是存储桶。
role.addToPolicy(new iam.PolicyStatement({
resources: [`${bucket.bucketArn}/*`],
actions: ['s3:GetObject']
}))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.