AWS API GATEWAY配置以从Lambda返回二进制pdf文件

问题描述:

我想从AWS Lambda函数返回一个pdf,并且我使用API​​ Gateway从任何浏览器中调用它.

I want to return a pdf from a AWS Lambda function, and I use API Gateway for calling it from a any browser.

我在c#中有一个aws lambda函数,该函数返回一个API网关响应,其主体是 base64编码的字符串中的 pdf .

I have a aws lambda function in c# that returns an API Gateway Response which body is a pdf in base64 encoded string.

到目前为止,端点返回的文件扩展名为 .pdf ,但不是二进制文件.而是带有base64字符串的文本文件.

So far the endpoint returns a file with .pdf extension but not in binary. Instead, is a text file with the base64 string.

我从C#代码返回的标头是:

The headers i'm returning from the c# code are:

  var headersDic = new Dictionary<string, string>();
      headersDic.Add("Content-type", "application/pdf");
      headersDic.Add("Content-disposition", "attachment; filename=file.pdf");

我手动将base64字符串转换为二进制文件并以pdf格式打开,并且可以正常工作,我的意思是base64字符串是正确的,因此我认为问题出在API网关上.

I converted manually the base64 string to binary file and opened it as a pdf, and it works, I mean, the base64 string is correct so I assume the problem is being the API Gateway.

在API网关的集成响应控制台中,我得到了:

In the integration response console of the API Gateway, I got this:

但是我无法使其正常工作.

But i'm not able to make it work.

我还启用了二进制媒体类型.

I also have binary media types enabled.

响应标头

'Content-Type': 'application/pdf'
'Content-disposition', 'attachment; filename=fileName.pdf'

回复客户

{
 statusCode: 200
 headers: responseHeader,
 body: pdfContent.toString(base64),
 isBase64Encoded: true,
}

API网关二进制媒体类型

API Gateway Binary Media Types

希望这会有所帮助