.NET版本的OSS的web直传,一直报签名错误(SignatureDoesNotMatch)
分享一个基于阿里云OSS SDK来生成签名的方法代码示例,供参考:
///
/// 生成浏览器端签名
///
/// 上传的目标文件夹
/// 签名信息
public OssSignature SignGen(string dir)
{
var now = DateTime.Now;
var ex = now.AddMinutes(30);
var policyCods = new PolicyConditions();
policyCods.AddConditionItem('content-length-range', 0L, 1048576000L);
policyCods.AddConditionItem(MatchMode.StartWith, 'key', dir);
var postPolicy = _ossClient.GeneratePostPolicy(ex, policyCods);
var binaryData = Encoding.Default.GetBytes(postPolicy);
var encodedPolicy = Convert.ToBase64String(binaryData);
var hmac = new HMACSHA1(Encoding.UTF8.GetBytes('你的AccessKeySecret'));
var hashBytes = hmac.ComputeHash(Encoding.UTF8.GetBytes(encodedPolicy));
var signature = Convert.ToBase64String(hashBytes);
DateTime startTime = CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
return new OssSignature
{
AccessId = XdpOssConfig.AliyunOssConfig.AccessKeyId,
Policy = encodedPolicy,
Signature = signature,
Dir = dir,
EndPoint = XdpOssConfig.AliyunOssConfig.EndPoint,
Expire = (long)(ex - startTime).TotalMilliseconds
};
}
赞1
踩0