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

AWS Lambda授权器配置后无法返回application/zip格式响应问题

解决添加Lambda授权器后无法下载ZIP文件的问题

问题根源

添加Lambda授权器后,API Gateway未正确识别application/zip为二进制媒体类型,默认将Lambda的响应序列化为JSON返回,而非解析并返回二进制ZIP文件。

解决方案

1. 配置API Gateway二进制媒体类型

  • 登录AWS控制台,打开API Gateway服务,选择目标API
  • 切换到Settings标签页,在Binary Media Types区域添加application/zip(若需兼容更多场景可追加application/octet-stream)
  • 保存设置后,重新将API部署到对应的阶段(务必重新部署,否则配置不生效)

2. 检查Lambda授权器响应格式

确保授权器返回标准结构,不要干扰主Lambda的响应头:

{
  "principalId": "user-id",
  "policyDocument": {
    "Version": "2012-10-17",
    "Statement": [
      {
        "Action": "execute-api:Invoke",
        "Effect": "Allow",
        "Resource": "arn:aws:execute-api:region:account-id:api-id/stage/method/resource-path"
      }
    ]
  }
}
  • 不要在授权器响应中返回Content-Type等会覆盖主响应的头信息
  • 若需传递上下文数据,仅在context字段中添加,避免影响响应处理逻辑

3. 验证API集成响应映射

  • 进入目标API的GET资源方法,切换到Integration Response标签
  • 找到200状态码对应的响应设置,确保:
    • Content-Type未被强制设置为application/json
    • 映射模板选择Passthrough(直接传递Lambda的响应内容和头),而非自定义JSON模板

4. 优化Lambda函数响应结构

修正响应头的冗余配置,避免API Gateway解析异常:

exports.handler = async (event) => {
  const zipBase64Content = "UEsFBgAAAAAAAAAAAAAAAAAAAAAAAA==";
  
  return {
    statusCode: 200,
    isBase64Encoded: true,
    body: zipBase64Content,
    headers: {
      "Content-Type": "application/zip",
      "Content-Disposition": "attachment; filename=Testfile.zip"
    }
  };
};
  • 移除Content-Encoding: base64:API Gateway会自动识别isBase64Encoded: true并处理解码,无需额外声明
  • 统一Content-Type为单一值,避免多值导致的识别冲突

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 15:25:16