javascript S3使用aws-sdk v3给出PutObject命令预签名url的SignatureDoesNotMatch错误

1aaf6o9v  于 2022-12-28  发布在  Java
关注(0)|答案(2)|浏览(158)

我的Amazon S3 Bucket工作正常,直到我决定将我的AWS SDK从版本V2更新到模块化V3。
我可以使用sdk编程上传文件,但我不能使用它生成的预签名url上传文件。

const { getSignedUrl } = require('@aws-sdk/s3-request-presigner');
      const { S3Client, , PutObjectCommand } = require('@aws-sdk/client-s3');
      const s3Client = S3Client({ region: 'us-east-2'});
      const params = {
        Bucket: '<bucket>',
        Key: '1234567890.jpg',
        ACL: 'private',
        ContentType: 'image/jpg',
        // Body: '<base64 encoded image content>'
      };
      const command = new PutObjectCommand(params);
      // await s3Client.send(command); // works fine
      const signedUrl = await getSignedUrl(s3Client, command); // generated signed url fails to upload image

当我尝试使用生成的预签名URL进行PUT请求时,我收到403 HTTP错误代码和消息SignatureDoesNotMatch。请指导我可能遗漏了什么,因为我已经为此工作了两天。

58wvjzkj

58wvjzkj1#

你试过这个吗;
https://www.digitalocean.com/community/questions/signature-does-not-match-when-putting-to-presigned-spaces-upload-url
这里有一个链接,了解更多详细信息;
https://www.msp360.com/resources/blog/s3-pre-signed-url-guide/

sc4hvdpw

sc4hvdpw2#

在我创建了一个具有程序访问权限(aws-access-key-id、aws-secret-access-key)的IAM用户,并在put images中附加了一个bucket策略之后,我的用户才开始工作。

{
"Version": "2012-10-17",
"Statement": [
    {
        "Sid": "VisualEditor0",
        "Effect": "Allow",
        "Action": "s3:PutObject",
        "Resource": "arn:aws:s3:::bucket-name/*"
    }
]

}
我还将此IAM用户的凭据添加到config

const credentials = { accessKeyId, secretAccessKey }
const params = { credentials, region }

我还将CORS规则应用于铲斗,如下所示:

[
{
    "AllowedHeaders": [],
    "AllowedMethods": [
        "GET"
    ],
    "AllowedOrigins": [
        "*"
    ],
    "ExposeHeaders": [],
    "MaxAgeSeconds": 0
},
{
    "AllowedHeaders": [
        "*"
    ],
    "AllowedMethods": [
        "PUT"
    ],
    "AllowedOrigins": [
        "*"
    ],
    "ExposeHeaders": [],
    "MaxAgeSeconds": 3600
}

]
我不需要添加ACL,内容类型到配置。

相关问题