React Native使用AWS Amplify调用Storage.get()预签名URL凭证为undefined
问题现象
- 已在React Native项目中集成
aws-amplify@3.3.4的Auth、Storage模块,用户通过Auth完成登录认证后,调用Storage.get('public/fileName.gif', { expires: 120 })获取文件预签名URL - 生成的预签名URL中
X-Amz-Credential字段固定为undefined,示例:
https://expyhealth-stg.s3.amazonaws.com/public/activityImages/Ankle%20Pumps.gif?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Credential=undefined%2F20201020%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20201020T204616Z&X-Amz-Expires=120&X-Amz-Signature=f40bad142a9b190f9d9959bb9db0ad077c0cecab5171098f033a700cb9aa45b5&X-Amz-SignedHeaders=host&x-amz-user-agent=aws-sdk-js-v3-react-native-%40aws-sdk%2Fclient-s3%2F1.0.0-gamma.8%20aws-amplify%2F3.6.0%20react-native&x-id=GetObject
- 已确认关联S3存储桶的策略允许对应Cognito已认证角色的
s3:GetObject权限,代码运行无未捕获异常 - 业务侧的Amplify封装、配置代码如下:
- 模块封装代码:
import Amplify, {Auth, Storage} from 'aws-amplify' import config from './config' Amplify.configure({ Auth: config.Auth, Storage: config.Storage, }) export { Auth, Storage, }- 配置源内容:
const config = { AWS: { Auth: { region: 'us-east-X', userPoolId: 'us-east-XXXXXXX', userPoolWebClientId: 'XXXXXXXXX', identityPoolId: 'us-east-X:XXXXXXXXXXXXXX', }, Storage: { AWSS3: { bucket: 'XXXXXXX', region: 'us-east-X', }, }, } }- S3存储桶访问策略内容:
{ "Version": "2012-10-17", "Id": "Policy1599854584652", "Statement": [ { "Sid": "Stmt1599854581275", "Effect": "Allow", "Principal": { "AWS": "arn:aws:iam::XXXXXXXXXXXX:role/Cognito_ExpyHealthStagingAuth_Role" }, "Action": "s3:GetObject", "Resource": [ "arn:aws:s3:::XXXXXXXX", "arn:aws:s3:::XXXXXXXX/*" ] } ] }
根因说明
核心问题为配置路径读取错误,导致Amplify根本没有拿到Auth、Storage的有效配置:
- 你当前的config对象结构为
{ AWS: { Auth: {}, Storage: {} } },但代码中读取配置的路径是config.Auth、config.Storage,实际取值为undefined - 等于你传给
Amplify.configure()的Auth、Storage配置全是空值,Auth模块因为没有拿到有效的identityPoolId配置,哪怕用户在UserPool层完成了登录,也不会自动向Cognito身份池请求对应IAM角色的临时访问密钥 - Storage模块生成预签名URL时,从Auth会话中读取到的访问密钥字段为空,直接输出
undefined拼接到URL参数里,最终出现观察到的X-Amz-Credential=undefined的现象 - S3桶策略不存在问题:问题发生在前端生成签名的阶段,还未到S3的权限校验环节,策略配置错误不会导致Credential字段取值为undefined
- 附加版本问题:
aws-amplify@3.3.4版本内置的@aws-sdk/client-s3为早期gamma预览版,存在已知bug:即使配置正确,偶发不会从Auth会话中注入临时凭证到S3签名流程中,也会出现Credential为undefined的问题
修复步骤
- 修正配置读取路径,将Auth、Storage配置按正确结构传入Amplify.configure()
调整后代码示例:import Amplify, {Auth, Storage} from 'aws-amplify' // 不再嵌套AWS父级对象,Auth、Storage直接作为顶级配置项 const amplifyConfig = { Auth: { region: 'us-east-X', userPoolId: 'us-east-XXXXXXX', userPoolWebClientId: 'XXXXXXXXX', identityPoolId: 'us-east-X:XXXXXXXXXXXXXX', }, Storage: { AWSS3: { bucket: 'XXXXXXX', region: 'us-east-X', }, } } Amplify.configure(amplifyConfig) export { Auth, Storage } - 升级依赖到兼容稳定版
将aws-amplify从3.3.4升级到3.4.4及以上的3.x系列稳定版,替换掉内置的早期gamma版S3 SDK,解决偶发凭证未注入的问题 - 校验Cognito身份池的角色映射
确认关联的identityPool中,已将「已认证用户」的角色关联到S3策略中配置的Cognito_ExpyHealthStagingAuth_Role,避免拿到有效凭证后仍无S3访问权限 - 流程验证
完成调整后,重新登录获取新的Auth会话,再次调用Storage.get()接口,检查生成的URL中X-Amz-Credential字段是否为格式正常的[访问密钥]/[日期]/[区域]/s3/aws4_request结构,确认签名生成正常
内容的提问来源于stack exchange,提问作者Joel Worsham
相关产品推荐
相关产品推荐

