AWS lambda 不返回二进制内容



我有一个API,我正在使用CDK将其部署到AWS。我使用的是lambda代理集成。我正在尝试创建一个新的API调用,它将返回一个zip文件,但我无法让它返回二进制数据。这是我的:

这最终将从浏览器链接调用,因此根据此文档,我将binaryMediaTypes设置为*/*(我也尝试将其设置为application/zip(:

const api = new apigateway.RestApi(this, "cursive-api", {
restApiName: props.apiName,
... etc etc ...
binaryMediaTypes: ["*/*"]
});

我的集成已将contentHandling设置为CONVERT_TO_BINARY:

const downloadIntegration = new apigateway.LambdaIntegration(downloadFn,
{
proxy: true,
contentHandling: apigateway.ContentHandling.CONVERT_TO_BINARY
});

我用curl打这个,并传递Accept标题:

curl -i -H "Accept: application/zip" <url>

我可以在CloudWatch中看到,accept标头正在通过CloudFront ok:

Field           Value
@ingestionTime  1620087566765
@log            592838288812:API-Gateway-Execution-Logs_xxx/prod
@logStream      xxx
@message        (xxx) Method request headers: {Accept=application/zip, CloudFront-Viewer-Country=NZ, CloudFront-Forwarded-Proto=https, CloudFront-Is-Tablet-Viewer=false, CloudFront-Is-Mobile-Viewer=false, User-Agent=curl/7.64.1, X-Forwarded-Proto=https, CloudFront-Is-SmartTV-Viewer=false, Host=staging-api.cursive-ide.com, X-Forwarded-Port=443, X-Amzn-Trace-Id=Root=1-xxx, Via=2.0 xxx.cloudfront.net (CloudFront), X-Amz-Cf-Id=Guy-_xxx==, X-Forwarded-For=151.210.253.227, 130.176.150.98, CloudFront-Is-Desktop-Viewer=true}
@timestamp      1620087546597

我还可以在CloudWatch中看到,我正在将其设置为Base64Encoded,并且内容类型是正确的:

Field           Value
@ingestionTime  1620087566765
@log            592838288812:API-Gateway-Execution-Logs_xxx/prod
@logStream      xxx
@message        (xxx) Endpoint response body before transformations: {"statusCode":200,"headers":{"content-type":"application/zip","Content-Disposition":"attachment; filename="licences.zip"","access-control-allow-origin":"https://<url>"},"isBase64Encoded":true,"body":"UEsDBETCETCETC [TRUNCATED]
@timestamp      1620087548798

然而,无论我做什么,我都只能得到base64编码的字符串。

LambdaIntegration构造上的

contentHandling仅适用于请求。您需要在集成响应上设置contentHandling

根据LambdaIntegrationOptions.contentHandling:文件

指定如何处理请求负载内容类型转换。

若要修复此问题,请在LambdaIntegrationOptions.integrationResponses上为200状态代码配置contentHandling

https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_apigateway.IntegrationResponse.html

最新更新