我试图根据这篇文章将文件上传到S3: https://aws.amazon.com/articles/1434
但是,我收到以下错误: "我们计算的请求签名与您提供的签名不匹配。检查您的密钥和签名方法。"
显然我没有正确计算签名,但我无法理解错误的位置。
我使用C#后端进行签名计算:
这是我的后端代码:
string policyStr = @"{""expiration"":""2014-05-01T12:00:00.000Z"",""conditions"": [{""bucket"":""my-bucket""},[""starts-with"",""$key"",""uploads/${filename}""],{""acl"":""private""},{""success_action_redirect"":""http://localhost/""},[""starts-with"",""$Content-Type"",""text/plain""],[""content-length-range"",0,1048576]]}";
string secretKey = "secret";
string bucketName = "my-bucket";
string filePath = string.Format(@"courseFiles\{0}", model.Name);
var hash = new HMACSHA1(Encoding.UTF8.GetBytes(secretKey));
var data = hash.ComputeHash(Encoding.UTF8.GetBytes(policyStr));
var signature = Convert.ToBase64String(data);
return new UploadFileResponseViewModel
{
AWSAccessKey = "accessKey",
BucketName = bucketName,
FilePath = filePath,
Policy = Convert.ToBase64String(Encoding.UTF8.GetBytes(policyStr)),
Signature = signature,
Action = "https://my-bucket.s3.amazonaws.com/"
};
客户端看起来与我提到的文章中解释的几乎相同。有谁知道我做错了什么?
答案 0 :(得分:1)
最后我找到了解决方案。
这是计算签名的正确方法,以防万一有需要。
string policyStr = @"{""expiration"":""2014-05-01T12:00:00.000Zr"",""conditions"":[{""bucket"":""my-bucket""},[""starts-with"",""$key"",""uploads""],{""acl"":""private""},{""success_action_redirect"":""http://localhost""},[""starts-with"",""$Content-Type"",""text/plain""],[""content-length-range"",0,1048576]]}";
string secretKey = "secret";
string bucketName = "my-bucket";
string filePath = string.Format(@"courseFiles\{0}", model.Name);
var hash = new HMACSHA1(Encoding.UTF8.GetBytes(secretKey));
var policy = Convert.ToBase64String(Encoding.UTF8.GetBytes(policyStr));
var data = hash.ComputeHash(Encoding.UTF8.GetBytes(policy));
var signature = Convert.ToBase64String(data);
return new UploadFileResponseViewModel
{
AWSAccessKey = "access",
BucketName = bucketName,
FilePath = filePath,
Policy = policy,
Signature = signature,
Action = "https://my-bucket.s3.amazonaws.com/"
};