You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AWS Serverless中Node.js Lambda下载PDF的settings.yaml配置咨询

Got it, let's translate those console-based PDF download settings into your Serverless Framework settings.yaml file. I'll map each of your exact steps directly into the YAML configuration below:

PDF Download Configuration for Serverless Framework

1. Add Content-Type: application/pdf Response Header

This corresponds to adding the header in API Gateway's method response. Define it directly under your function's HTTP event to ensure the response includes the correct content type:

functions:
  yourPdfDownloadFunction: # Replace with your actual function name
    handler: handler.downloadPdf # Replace with your handler path
    events:
      - http:
          path: download/pdf # Replace with your endpoint path
          method: get
          # Configure method response headers
          response:
            statusCodes:
              200:
                headers:
                  Content-Type: true # Expose this header in the response

2. Map application/pdf Response Model to Empty

This tells API Gateway not to parse the PDF response as JSON. You'll define the Empty model and link it to the application/pdf content type in the custom API Gateway settings:

custom:
  apiGateway:
    # Define the Empty response model
    models:
      Empty:
        name: Empty
        contentType: application/pdf
        schema: {} # No schema needed for binary responses
    # Link the model to your 200 OK response
    responses:
      200:
        models:
          application/pdf: Empty

3. Set Content Handling to Convert to Binary

This is the integration response setting that tells API Gateway to pass the raw binary PDF data instead of modifying it. Add this to your function's HTTP event integration configuration:

functions:
  yourPdfDownloadFunction:
    handler: handler.downloadPdf
    events:
      - http:
          path: download/pdf
          method: get
          integration: lambda
          # Configure integration response content handling
          integrationResponse:
            statusCodes:
              200:
                contentHandling: CONVERT_TO_BINARY
                # Ensure the Content-Type header is set correctly
                responseParameters:
                  method.response.header.Content-Type: "'application/pdf'"

Critical Additional Setting

Don't forget to mark application/pdf as a binary media type for API Gateway—this ensures it doesn't try to encode the PDF as text. Add this to your custom section:

custom:
  apiGateway:
    binaryMediaTypes:
      - 'application/pdf'

Quick Lambda Handler Note

Make sure your Lambda function returns the PDF as a Buffer (or base64-encoded string with the appropriate isBase64Encoded flag set). For example:

// Sample handler snippet
exports.downloadPdf = async () => {
  const pdfBuffer = await getYourPdfData(); // Replace with your PDF fetch logic
  return {
    statusCode: 200,
    headers: { 'Content-Type': 'application/pdf' },
    body: pdfBuffer.toString('base64'),
    isBase64Encoded: true
  };
};

内容的提问来源于stack exchange,提问作者Anil Bhaskaran

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 09:54:29