【问题标题】:s3 isn't uploading file and getting error of SignatureDoesNotMatchs3 未上传文件并收到 SignatureDoesNotMatch 错误
【发布时间】:2019-06-05 06:24:03
【问题描述】:

我正在尝试将图像添加到我在 aws 中的 s3 存储桶,但它似乎不起作用。我收到SignatureDoesNotMatch的错误

这是我上传文件/图像的方式:

前端

const file = e.target.files[0];
const fileParts = file.name.split('.');
const fileName = fileParts[0];
const fileType = fileParts[1];
const response = axios.post('api/aws/sign_s3', { fileName, fileType );

后台

router.post('/sign_s3', async (req, res) => {
    aws.config.update({
        accessKeyId: config.aws.accessKey,
        secretAccessKey: config.aws.secretKey,
        region: 'us-west-1'
    });

    const s3 = new aws.S3(); // Create a new instance of S3
    const fileName = req.body.fileName;
    const fileType = req.body.fileType;

    const s3Params = {
        Bucket: config.aws.bucketName,
        Key: fileName,
        Expires: 500,
        ContentType: fileType,
        ACL: 'public-read'
    };

    s3.getSignedUrl('putObject', s3Params, (err, data) => {
        if (err) return res.send(err);

        const returnData = {
            signedRequest: data,
            url: `https://sim-to-do.s3.amazonaws.com/${fileName}`
        };
        res.json({ success: true, responseData: returnData });
    });

});

我得到两个网址。当我转到第一个时,我收到以下错误代码:

SignatureDoesNotMatch

错误信息

我们计算的请求签名与您提供的签名不匹配。检查您的密钥和签名方法。

我做错了什么?将文件上传到 aws s3 的正确方法是什么?

【问题讨论】:

  • 问题是您将扩展名设置为ContentType,这在定义上是错误的。例如。而不是jpg,您应该将image/jpg 设置为ContentType

标签: javascript node.js amazon-web-services express amazon-s3


【解决方案1】:

从标题中删除 Content-Type 后,我能够解决此问题。

【讨论】:

    【解决方案2】:

    如果您收到“签名不匹配”,很可能您使用了错误的秘密访问密钥。您能否再次检查访问密钥和秘密访问密钥以确保它们正确无误?

    来自awendtanswer

    【讨论】:

      猜你喜欢
      • 2018-09-19
      • 2012-05-02
      • 2021-12-04
      • 2014-02-03
      • 2011-12-20
      • 2018-05-03
      • 2016-03-23
      • 2017-04-30
      • 2016-11-02
      相关资源
      最近更新 更多