I'm trying to upload a file to S3 based on this article: https://aws.amazon.com/articles/1434
However, I get the following error: "The request signature we calculated does not match the signature you provided. Check your key and signing method."
Obviously I do not calculate the signature correctly, but I cannot understand where's the error.
I'm using C# backend for signature calculations:
Here's my backend code:
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/"
};
The client side looks almost the same as the one explained in the article I mentioned. Does anyone knows what I'm doing wrong?
Finally I found the solution.
Here's the correct way to calculate the signature just in case anyone needs it.
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/"
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.