Skip to content

Commit 5532c7d

Browse files
committed
修复授权给第三方下载文件的url生成问题。
1 parent 5b3ca55 commit 5532c7d

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

src/OssClient.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -236,19 +236,26 @@ public async Task<OssResult<GetObjectMetaResult>> GetObjectMetaAsync(BucketInfo
236236
/// <summary>
237237
/// 获取文件的下载链接
238238
/// </summary>
239-
/// <param name="storeKey"></param>
240-
/// <param name="expireSeconds"></param>
239+
/// <param name="bucket">bucket信息</param>
240+
/// <param name="storeKey">文件存储key</param>
241+
/// <param name="expireSeconds">签名超时时间秒数</param>
242+
/// <param name="imgStyle">阿里云图片处理样式</param>
241243
/// <returns></returns>
242-
public string GetFileDownloadLink(BucketInfo bucket, string storeKey, int expireSeconds)
244+
public string GetFileDownloadLink(BucketInfo bucket, string storeKey, int expireSeconds, string imgStyle = null)
243245
{
244-
long seconds = (DateTime.UtcNow.Ticks - 621355968000000000) / 10000000;
246+
long seconds = (DateTime.UtcNow.AddSeconds(expireSeconds).Ticks - 621355968000000000) / 10000000;
245247

246248
string toSign = String.Format("GET\n\n\n{0}\n/{1}/{2}", seconds, bucket.BucketName, storeKey);
249+
if (!String.IsNullOrEmpty(imgStyle))
250+
{
251+
toSign += $"?x-oss-process=style/{imgStyle}";
252+
}
247253

248254
string sign = ServiceSignature.Create().ComputeSignature(
249-
_requestContext.OssCredential.AccessKeyId, toSign);
255+
_requestContext.OssCredential.AccessKeySecret, toSign);
250256

251-
string url = $"{bucket.BucketUri}/{storeKey}?OSSAccessKeyId={_requestContext.OssCredential.AccessKeyId}&Expires={seconds}&Signature={WebUtility.UrlEncode(sign)}";
257+
string styleSegment = String.IsNullOrEmpty(imgStyle) ? String.Empty : $"x-oss-process=style/{imgStyle}&";
258+
string url = $"{bucket.BucketUri}{storeKey}?{styleSegment}OSSAccessKeyId={_requestContext.OssCredential.AccessKeyId}&Expires={seconds}&Signature={WebUtility.UrlEncode(sign)}";
252259

253260
return url;
254261
}

src/Utility/Authentication/HmacSHA1Signature.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,17 @@ protected override string ComputeSignatureCore(string key, string data)
3232
// algorithm.ComputeHash(_encoding.GetBytes(data.ToCharArray())));
3333
//}
3434

35-
using (var algorithm = new HMACSHA1(_encoding.GetBytes(key.ToCharArray())))
35+
var bytes = _encoding.GetBytes(key);
36+
Trace.WriteLine("Key:" + key + " Data:" + data);
37+
Trace.WriteLine(bytes);
38+
39+
using (var algorithm = new HMACSHA1(_encoding.GetBytes(key)))
3640
{
3741
//algorithm.Key = _encoding.GetBytes(key.ToCharArray());
38-
return Convert.ToBase64String(
42+
var result = Convert.ToBase64String(
3943
algorithm.ComputeHash(_encoding.GetBytes(data.ToCharArray())));
44+
Trace.WriteLine("Result:" + result);
45+
return result;
4046
}
4147
}
4248
}

0 commit comments

Comments
 (0)