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

AWS S3 JS SDK调用getSignedUrl时报错:Cannot read 'sso_session' of undefined

问题:调用AWS S3 JS SDK的getSignedUrl时触发TypeError: Cannot read properties of undefined (reading 'sso_session')

执行以下两段AWS S3 JS SDK代码时,每次调用getSignedUrl都会触发错误:

代码1(生成上传预签名URL)

//preparing upload parameters
const params = {
  Bucket: `${config.s3bucket}`,
  Key: `${objectDirectory}/${objectKey}`,
};

//generating signed url, valid only for 15 minutes
const command = new PutObjectCommand(params);
const signedUrl = await getSignedUrl(s3Client, command, {
  expiresIn: 900,
});

代码2(生成下载预签名URL)

const bucketParams = {
  Bucket: config.s3bucket,
  Key: `Profiles/${result.profilePicture}`,
};
const command = new GetObjectCommand(bucketParams);
const urls = await getSignedUrl(s3Client, command, {
  expiresIn: 60 * 60 * 168,
});

错误信息

{
    "message": "TypeError: Cannot read properties of undefined (reading 'sso_session')"
}

解决办法

1. 检查AWS配置文件的SSO会话配置

这个错误大概率是S3客户端自动解析AWS配置文件(如~/.aws/config或~/.aws/credentials)时,遇到了未定义的sso_session字段。

  • 若使用SSO凭证:确保~/.aws/config中sso_session对应的会话已通过aws sso login完成登录,且会话配置的sso_start_url、sso_region等参数正确。
  • 若不需要SSO:删除配置文件中多余的SSO相关配置项,避免客户端尝试加载无效的SSO会话。

2. 显式指定凭证提供者

初始化S3Client时,显式指定凭证来源,跳过自动解析逻辑:

import { S3Client } from "@aws-sdk/client-s3";
import { fromEnv } from "@aws-sdk/credential-provider-env";

// 从环境变量加载凭证(生产环境推荐用IAM角色,测试可手动设置AWS_ACCESS_KEY_ID等变量)
const s3Client = new S3Client({
  credentials: fromEnv(),
  region: "your-aws-region" // 替换为你的AWS区域,如us-east-1
});

或者指定配置文件中的特定profile:

import { fromIni } from "@aws-sdk/credential-provider-ini";

const s3Client = new S3Client({
  credentials: fromIni({ profile: "your-profile-name" }),
  region: "your-aws-region"
});

3. 排查冲突的环境变量

检查是否设置了AWS_CONFIG_FILE指向存在问题的配置文件,或AWS_SSO_SESSION指定了不存在的会话名称,如有则调整或删除这些环境变量。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 12:20:54