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

AWS Lambda调用S3 getObject报错:'Not a function'求助

解决AWS Lambda中S3 SDK v3 getObject is not a function 错误

问题根源

AWS SDK v3采用模块化架构,和v2的API调用逻辑完全不同:

  • v3的S3Client本身没有getObject方法,必须使用对应的GetObjectCommand类发起请求
  • v3不再支持回调模式,统一通过async/await处理异步操作

修正后的代码示例

import { S3Client, GetObjectCommand } from '@aws-sdk/client-s3';

export const handler = async (event) => {
  // 初始化S3客户端
  const s3 = new S3Client({
    region: 'us-east-1'
  });

  // 构建GetObject命令参数
  const params = {
    Bucket: 'integration-maps',
    Key: 'testFile.json'
  };

  try {
    // 执行命令并获取响应
    const response = await s3.send(new GetObjectCommand(params));
    // 将响应Body转换为字符串并解析为JSON
    const jsonContent = await response.Body.transformToString();
    const data = JSON.parse(jsonContent);
    
    console.log('文件内容:', data);
    return {
      statusCode: 200,
      body: JSON.stringify(data)
    };
  } catch (err) {
    console.error('读取S3文件失败:', err);
    return {
      statusCode: 500,
      body: JSON.stringify({ error: err.message })
    };
  }
};

额外排查点

  • 权限配置:确保Lambda的执行角色拥有s3:GetObject权限,示例策略:
    {
      "Version": "2012-10-17",
      "Statement": [
        {
          "Effect": "Allow",
          "Action": "s3:GetObject",
          "Resource": "arn:aws:s3:::integration-maps/testFile.json"
        }
      ]
    }
    
  • 依赖安装:确认已执行npm install @aws-sdk/client-s3安装正确依赖
  • Command导入:必须同时导入S3Client和GetObjectCommand,不要遗漏后者

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 05:52:37