使用 CDK 创建可以读取非 public S3 存储桶的 API 网关
Create API Gateway that can read non-public S3 bucket using CDK
我正在尝试使用可以访问非public 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 存储桶 public。我认为这是因为它向 S3 发送了 http 请求,但我只允许 s3:GetObject
在角色中。
那么,我如何更改 API 网关以允许访问 S3 存储桶对象而不使它们成为 public。
我尝试了以下代码,它看起来很有前途,因为它说明了操作 GetObject
,但它没有用。
api.root
.addResource('{file}')
.addMethod('GET', new apigw.AwsIntegration({
service: 's3',
action: 'GetObject',
actionParameters: {
Bucket: 'my-test-bucket',
Key: 'file'
},
...
您的保单有问题。 GetObject
权限应应用于对象而不是存储桶。
role.addToPolicy(new iam.PolicyStatement({
resources: [`${bucket.bucketArn}/*`],
actions: ['s3:GetObject']
}))
我正在尝试使用可以访问非public 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 存储桶 public。我认为这是因为它向 S3 发送了 http 请求,但我只允许 s3:GetObject
在角色中。
那么,我如何更改 API 网关以允许访问 S3 存储桶对象而不使它们成为 public。
我尝试了以下代码,它看起来很有前途,因为它说明了操作 GetObject
,但它没有用。
api.root
.addResource('{file}')
.addMethod('GET', new apigw.AwsIntegration({
service: 's3',
action: 'GetObject',
actionParameters: {
Bucket: 'my-test-bucket',
Key: 'file'
},
...
您的保单有问题。 GetObject
权限应应用于对象而不是存储桶。
role.addToPolicy(new iam.PolicyStatement({
resources: [`${bucket.bucketArn}/*`],
actions: ['s3:GetObject']
}))