我们计算的请求签名与您提供的签名不匹配

时间:2015-05-15 15:14:32

标签: c# amazon-web-services amazon-s3 cors

我想通过asp.net项目将我的视频文件从浏览器上传到Amazon S3存储。但我不断收到此错误:“我们计算的请求签名与您提供的签名不匹配”

这是我的javascript代码;

function uploadFile() {

var access_key = "xx";
var secret = "xx";
var policy = "xx";
var signature = "xx";

var file = document.getElementById('file').files[0];
var fd = new FormData();

var key = "folder1/" + (new Date).getTime() + '-' + file.name;

fd.append('key', key);
fd.append('acl', 'public-read-write');
fd.append('Content-Type', file.type);
fd.append('AWSAccessKeyId', access_key);
fd.append('policy', policy)
fd.append('signature', signature);

fd.append("file", file);

var xhr = new XMLHttpRequest();

xhr.upload.addEventListener("progress", uploadProgress, false);
xhr.addEventListener("load", uploadComplete, false);
xhr.addEventListener("error", uploadFailed, false);
xhr.addEventListener("abort", uploadCanceled, false);

xhr.open('POST', 'https://bucketName.s3.amazonaws.com/', true);

xhr.send(fd);

}

这是政策字符串;

{" +
                      "\"Id\": \"Policyxxx\"," +
                      "\"Version\": \"2012-10-17\"," +
                      "\"Statement\": [" +
                        "{" +
                          "\"Sid\": \"Stmtxxx\"," +
                          "\"Action\": \"s3:*\"," +
                          "\"Effect\": \"Allow\"," +
                          "\"Resource\": \"arn:aws:s3:::bucketName/*\"," +
                          "\"Principal\": \"*\"" +
                        "}" +
                      "]" +
                    "}

这是getsignature方法;

 public static string GetS3Signature(string policyStr)
    {
        string b64Policy = Convert.ToBase64String(Encoding.ASCII.GetBytes(policyStr));

        byte[] b64Key = Encoding.ASCII.GetBytes(AwsSecretKey);
        HMACSHA1 hmacSha1 = new HMACSHA1(b64Key);

        var c = Convert.ToBase64String(hmacSha1.ComputeHash(Encoding.ASCII.GetBytes(b64Policy)));
        return c;
    }

可能是错误的原因

1 个答案:

答案 0 :(得分:0)

政策中提及的任何条件也必须采用表格数据。例如,如果您有类似

的条件
["starts-with", "x-amz-meta-myelement", ""]

然后你必须发送那把钥匙。如果策略对内容类型或密钥前缀有约束,则这些也可能导致签名错误。