- Newest
- Most votes
- Most comments
您遇到的 S3 上传 400 错误可能有几个原因。根据您提供的代码和日志,我注意到几个可能的问题点:
-
端点配置问题:您使用了自定义端点
oss.........com
,但同时设置了config.useVirtualAddressing = false
。S3 默认使用虚拟寻址风格,如果您的端点需要路径风格寻址,这个设置是正确的,但可能与您的端点要求不匹配。 -
认证问题:日志中显示 "If the signature check failed. This could be because of a time skew",这表明可能存在签名验证失败的问题。虽然日志显示客户端和服务器时间似乎一致,但签名计算可能仍有问题。
-
硬编码凭证:您在代码中直接硬编码了 AWS 凭证,这不是最佳实践。在 Lambda 或其他 AWS 服务中运行时,应该使用执行角色的权限而不是硬编码凭证。
-
区域与端点不匹配:您设置了
us-east-1
区域,但使用了自定义端点。确保这两者是兼容的。 -
HTTP 协议:您使用了
Aws::Http::Scheme::HTTP
而不是 HTTPS,S3 通常需要 HTTPS 连接。
建议的解决方案:
- 尝试使用 HTTPS 而不是 HTTP:
config.scheme = Aws::Http::Scheme::HTTPS;
- 检查虚拟寻址设置是否与您的端点兼容:
config.useVirtualAddressing = true; // 或根据您的端点要求设置
- 如果在 AWS Lambda 中运行,移除硬编码凭证,让代码使用 Lambda 执行角色:
Aws::S3::S3Client s3_client(config);
-
确保区域设置与您的端点匹配。
-
检查您的桶名称和文件路径是否正确。
由于您提到可以使用 AWS CLI 成功上传,这表明权限和端点配置基本正确。问题可能在于 SDK 的具体配置或签名计算方式与 CLI 不同。尝试以上建议,特别是切换到 HTTPS 和调整虚拟寻址设置,可能会解决您的问题。
Sources
Use PutObject with an AWS SDK or CLI - Amazon Simple Storage Service
The request signature we calculated does not match the signature you provided. Check your key and signing method. I get this error only when i use lambda function. Their is no issue in local server | AWS re:Post
Use PutObject with an AWS SDK or CLI - AWS SDK Code Examples
if use
config.useVirtualAddressing = true;
then
Error: PutObject: curlCode: 6, Couldn't resolve host name; Details: Could not resolve host: cct--test.oss..com; Unknown error
The same return when using HTTPS, and the awl cli test uses the http address.
The time recorded in the logs is GMT, and the time on my local machine is CST,
which could be a problem, but I don't know how unify the time to CST,
the logs output by the aws cli are also CST time.
Relevant content
- asked a year ago
- asked 8 months ago
- asked 2 years ago