AWS S3直接上传返回无效的签名(第4版签名)C#(AWS S3 direct upload re

2019-09-30 19:38发布

我想直接从浏览器上传文件到Amazon S3。 我阅读文档,并遵循例如Amazon提供

示例方法工作,除了POST方法。 该错误信息是我们计算出你所提供的签名不匹配的请求签名。 检查你的密钥和签名方法。

我还检查该代码示例。 并应用了答案的代码,并仍然得到同样的错误。

public class PolicyBuilder
{
    public static string Key = "Key";
    public static string Secret = "Secret";

    public static string GetS3PolicySignatureV4()
    {
        var policyBuilder = new StringBuilder();

        policyBuilder.AppendFormat("{{ \"expiration\": \"{0}\",\r\n", "2018-12-30T12:00:00.000Z");
        policyBuilder.Append("  \"conditions\": [\r\n");
        policyBuilder.Append("    [\"starts-with\", \"$key\", \"\"],\r\n");
        policyBuilder.AppendFormat("    {{\"x-amz-credential\": \"{0}\"}},\r\n",  Key + "/20180308/us-east-1/s3/aws4_request");
        policyBuilder.Append("    {\"x-amz-algorithm\": \"AWS4-HMAC-SHA256\"},\r\n");
        policyBuilder.Append("    {\"x-amz-date\": \"20180308T000000Z\" }\r\n");
        policyBuilder.Append("  ]\r\n}");

        var policyString = policyBuilder.ToString();
        var policyStringBytes = Encoding.UTF8.GetBytes(policyString);
        var policy = Convert.ToBase64String(policyStringBytes);

        byte[] signingKey = GetSignatureKey(Key, "20180308", "us-east-1", "s3");
        byte[] signature = HmacSHA256(policy, signingKey);
        var sig = ToHexString(signature, true);
        return sig;
    }

    static byte[] HmacSHA256(String data, byte[] key)
    {
        String algorithm = "HmacSHA256";
        KeyedHashAlgorithm kha = KeyedHashAlgorithm.Create(algorithm);
        kha.Key = key;

        return kha.ComputeHash(Encoding.UTF8.GetBytes(data));
    }

    static byte[] GetSignatureKey(String key, String dateStamp, String regionName, String serviceName)
    {
        byte[] kSecret = Encoding.UTF8.GetBytes(("AWS4" + key).ToCharArray());
        byte[] kDate = HmacSHA256(dateStamp, kSecret);
        byte[] kRegion = HmacSHA256(regionName, kDate);
        byte[] kService = HmacSHA256(serviceName, kRegion);
        byte[] kSigning = HmacSHA256("aws4_request", kService);

        return kSigning;
    }

    public static string ToHexString(byte[] data, bool lowercase)
    {
        var sb = new StringBuilder();
        for (var i = 0; i < data.Length; i++)
        {
            sb.Append(data[i].ToString(lowercase ? "x2" : "X2"));
        }
        return sb.ToString();
    }
}

这里是表格:

 <!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> <head> <meta charset="utf-8" /> <title></title> </head> <body> <form action="http://MyBucket.s3.amazonaws.com/" method="post" enctype="multipart/form-data"> <input type="hidden" name="key" value="/images/a5827206-ea2c-4fc6-a66b-aebde27d0ea3.jpg" /> <input type="hidden" name="x-amz-credential" value="Key/20180308/us-east-1/s3/aws4_request" /> <input type="hidden" name="x-amz-algorithm" value="AWS4-HMAC-SHA256" /> <input type="hidden" name="x-amz-date" value="20180308T000000Z" /> <input type="hidden" name="policy" value='<Base64PolicyResult>' /> <input type="hidden" name="x-amz-signature" value="<GenerateSignature>" /> File: <input type="file" name="file" /> <br /> <input type="submit" name="submit" value="Upload to Amazon S3" /> </form> </body> </html> 

我仍然得到同样的错误。 我应该为我的Amazon S3的桶任何具体的政策? 有什么错在我的代码? :(

Answer 1:

该错误是在这里...

byte[] signingKey = GetSignatureKey(Key, "20180308", "us-east-1", "s3");

这条线应该是...

byte[] signingKey = GetSignatureKey(Secret, "20180308", "us-east-1", "s3");

您正在使用AWS-访问密钥ID,而不是秘密密钥生成的签名密钥。



文章来源: AWS S3 direct upload returns invalid signature (version 4 signature) C#