gpt4 book ai didi

c# - AWS S3 直接上传返回无效签名(版本 4 签名)

转载 作者:行者123 更新时间:2023-11-30 15:59:03 27 4
gpt4 key购买 nike

我讨厌看到这样的回复:

The request signature we calculated does not match the signature you provided. Check your key and signing method.

我知道这个问题已经在这里得到回答的版本:

SO - AWS S3 browser upload using HTTP POST gives invalid signature

而且我没有运气地遵循它的每个细节,而且我可能遗漏了一些简单的东西。我正在使用 C# 生成策略和 v4 Aws 签名。这是策略代码:

var policyBuilder = new StringBuilder();

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

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

这是用于生成签名的代码:

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();
}

将它们结合在一起的方法:

public string GetS3PolicySignatureV4(string policy)
{
byte[] signingKey = GetSignatureKey(<MySecretKey>, "20170214T000000Z", "us-east-2", "s3");
byte[] signature = HmacSHA256(policy, signingKey);
return AWS4SignerBase.ToHexString(signature, true);
}

这是 html 表单:

<form action="http://<BucketName>.s3.amazonaws.com/" method="post" enctype="multipart/form-data">
<input type="hidden" name="key" value="<FileKey>" />
<input type="hidden" name="x-amz-credential" value="<MyAccessKey>/20170214/us-east-2/s3/aws4_request"/>
<input type="hidden" name="x-amz-algorithm" value="AWS4-HMAC-SHA256" />
<input type="hidden" name="x-amz-date" value="20170214T000000Z" />
<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>

我已验证此示例中的示例策略结果:

AWS - Examples: Browser-Based Upload using HTTP POST (Using AWS Signature Version 4)

以及生成的签名与使用提供的参数和 key 的示例相匹配。但是当我尝试 POST 到 S3 时,我总是得到那种可怕的响应。

最佳答案

问题是传递给 GetSignatureKey 方法的日期格式不正确。应该刚好是“20170214”,隐藏表单域x-amz-date是“20170214T000000Z”的ISO8601格式。我将相同的值传递给 GetSignatureKey 方法。

关于c# - AWS S3 直接上传返回无效签名(版本 4 签名),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42237602/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com