将文件上传到s3服务器时出错(请使用AWS4-HMAC-SHA256)

时间:2018-07-10 09:19:54

标签: angularjs node.js amazon-web-services amazon-s3 nodes

错误消息:不支持您提供的授权机制。请使用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(签名和策略发送到angular 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
};

该问题如何解决?我尝试了所有可能的方法,但是没有用。

1 个答案:

答案 0 :(得分:2)

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

您的保单看起来像V4,但是您的签名和要发布的表格都是V2。 AWS4-SHA256-HMAC是Signature V4,并且错误消息表明您的存储桶位于仅支持 V4的区域中,因此您的代码都必须全部使用V4逻辑。

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