首页 > 解决方案 > 将文件上传到 s3 服务器时出错(请使用 AWS4-HMAC-SHA256)

问题描述

错误信息:不支持您提供的授权机制。请使用 AWS4-HMAC-SHA256。

Angular js http发布请求

 url: 'https://abc.s3.amazonaws.com/', //webAPI exposed to upload the file
       method: 'POST',
           data: {
              key: file.name,
              AWSAccessKeyId: '*****',
              acl: 'private',
              Policy: $scope.policy,
              Signature: $scope.signature,
             "Content-Type": file.type != '' ? file.type : 'application/octet-stream', // content type of the file (NotEmpty)
              file: file

Nodejs(签名和策略被发送到角度js中的范围变量)

var secretKey = '*******';
var s3Policy = {
    "expiration": "2018-12-01T12:00:00.000Z", // hard coded for testing
    "conditions": [
        { "bucket": "*****" },
        ["starts-with", "$key", ""],
        { "acl": "private" },
        ["starts-with", "$Content-Type", ""],
        {'x-amz-meta-uuid': '14365123651274'},
        {'x-amz-credential': '****/20181212/eu-west-3/s3/aws4_request'},
        {"x-amz-algorithm" : "AWS4-HMAC-SHA256"},
        ["content-length-range", 0, 524288000]
    ],
};
var stringPolicy = JSON.stringify(s3Policy);
var base64Policy = Buffer(stringPolicy, "utf-8").toString("base64");
var signature = crypto.createHmac("sha256", secretKey)
    .update(new Buffer(base64Policy, "utf-8")).digest("base64");
var s3Credentials = {
    s3Policy: base64Policy,
    s3Signature: signature
};

如何解决这个问题?我已经尝试了所有可能的方法,但它不起作用。

标签: angularjsnode.jsamazon-web-servicesamazon-s3nodes

解决方案


您正在混合和匹配来自 Signature V4 和 Signature V2 的元素。它们是不兼容的算法。

您的保单看起来像 V4,但您的签名和您发布的表格都是 V2。AWS4-SHA256-HMAC是Signature V4,错误信息表明你的bucket所在区域支持V4...所以你的代码需要全部使用V4逻辑。

查看https://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-post-example.html


推荐阅读