0

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?

1 Answer 1

1

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/"
        };
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.