1717using Cuiliang . AliyunOssSdk . Entites ;
1818using Cuiliang . AliyunOssSdk . Request ;
1919using Cuiliang . AliyunOssSdk . Utility ;
20+ using Cuiliang . AliyunOssSdk . Utility . Authentication ;
2021
2122namespace Cuiliang . AliyunOssSdk
2223{
@@ -51,9 +52,9 @@ public async Task<OssResult<ListBucketsResult>> ListBucketsAsync(string region)
5152 /// <param name="key"></param>
5253 /// <param name="file"></param>
5354 /// <returns></returns>
54- public async Task < OssResult < PutObjectResult > > PutObjectAsync ( BucketInfo bucket , string key , RequestContent file )
55+ public async Task < OssResult < PutObjectResult > > PutObjectAsync ( BucketInfo bucket , string key , RequestContent file , IDictionary < string , string > extraHeaders = null )
5556 {
56- var cmd = new PutObjectCommand ( _requestContext , bucket , key , file , null ) ;
57+ var cmd = new PutObjectCommand ( _requestContext , bucket , key , file , extraHeaders ) ;
5758
5859 return await cmd . ExecuteAsync ( _client ) ;
5960 }
@@ -66,16 +67,17 @@ public async Task<OssResult<PutObjectResult>> PutObjectAsync(BucketInfo bucket,
6667 /// <param name="content"></param>
6768 /// <param name="mimeType"></param>
6869 /// <returns></returns>
69- public async Task < OssResult < PutObjectResult > > PutObjectAsync ( BucketInfo bucket , string key , string content , string mimeType = "text/plain" )
70+ public async Task < OssResult < PutObjectResult > > PutObjectAsync ( BucketInfo bucket , string key , string content , string mimeType = "text/plain" , ObjectMetadata meta = null , IDictionary < string , string > extraHeaders = null )
7071 {
7172 var file = new RequestContent ( )
7273 {
7374 ContentType = RequestContentType . String ,
7475 StringContent = content ,
75- MimeType = mimeType
76+ MimeType = mimeType ,
77+ Metadata = meta
7678 } ;
7779
78- return await PutObjectAsync ( bucket , key , file ) ;
80+ return await PutObjectAsync ( bucket , key , file , extraHeaders ) ;
7981 }
8082
8183 /// <summary>
@@ -86,21 +88,45 @@ public async Task<OssResult<PutObjectResult>> PutObjectAsync(BucketInfo bucket,
8688 /// <param name="filePathName"></param>
8789 /// <returns></returns>
8890 public async Task < OssResult < PutObjectResult > > PutObjectByFileNameAsync ( BucketInfo bucket , string key ,
89- string filePathName )
91+ string filePathName , ObjectMetadata meta = null , IDictionary < string , string > extraHeaders = null )
9092 {
9193 using ( var stream = File . OpenRead ( filePathName ) )
9294 {
9395 var file = new RequestContent ( )
9496 {
9597 ContentType = RequestContentType . Stream ,
9698 StreamContent = stream ,
97- MimeType = MimeHelper . GetMime ( filePathName )
99+ MimeType = MimeHelper . GetMime ( filePathName ) ,
100+ Metadata = meta
98101 } ;
99102
100- return await PutObjectAsync ( bucket , key , file ) ;
103+ return await PutObjectAsync ( bucket , key , file , extraHeaders ) ;
101104 }
102105 }
103106
107+ /// <summary>
108+ /// 上传流
109+ /// </summary>
110+ /// <param name="bucket"></param>
111+ /// <param name="key"></param>
112+ /// <param name="content">内容流</param>
113+ /// <param name="mimeType"></param>
114+ /// <returns></returns>
115+ public async Task < OssResult < PutObjectResult > > PutObjectAsync ( BucketInfo bucket , string key , Stream content ,
116+ string mimeType = "application/octet-stream" , ObjectMetadata meta = null , IDictionary < string , string > extraHeaders = null )
117+ {
118+ var file = new RequestContent ( )
119+ {
120+ ContentType = RequestContentType . Stream ,
121+ StreamContent = content ,
122+ MimeType = mimeType ,
123+ Metadata = meta
124+ } ;
125+
126+ return await PutObjectAsync ( bucket , key , file , extraHeaders ) ;
127+ }
128+
129+
104130 /// <summary>
105131 /// 复制对象
106132 /// </summary>
@@ -202,5 +228,45 @@ public async Task<OssResult<GetObjectMetaResult>> GetObjectMetaAsync(BucketInfo
202228 var cmd = new GetObjectMetaCommand ( _requestContext , bucket , key ) ;
203229 return await cmd . ExecuteAsync ( _client ) ;
204230 }
231+
232+ /// <summary>
233+ /// 获取文件的下载链接
234+ /// </summary>
235+ /// <param name="bucket">bucket信息</param>
236+ /// <param name="storeKey">文件存储key</param>
237+ /// <param name="expireSeconds">签名超时时间秒数</param>
238+ /// <param name="imgStyle">阿里云图片处理样式</param>
239+ /// <returns></returns>
240+ public string GetFileDownloadLink ( BucketInfo bucket , string storeKey , int expireSeconds , string imgStyle = null )
241+ {
242+ long seconds = ( DateTime . UtcNow . AddSeconds ( expireSeconds ) . Ticks - 621355968000000000 ) / 10000000 ;
243+
244+ string toSign = String . Format ( "GET\n \n \n {0}\n /{1}/{2}" , seconds , bucket . BucketName , storeKey ) ;
245+ if ( ! String . IsNullOrEmpty ( imgStyle ) )
246+ {
247+ toSign += $ "?x-oss-process=style/{ imgStyle } ";
248+ }
249+
250+ string sign = ServiceSignature . Create ( ) . ComputeSignature (
251+ _requestContext . OssCredential . AccessKeySecret , toSign ) ;
252+
253+ string styleSegment = String . IsNullOrEmpty ( imgStyle ) ? String . Empty : $ "x-oss-process=style/{ imgStyle } &";
254+ string url = $ "{ bucket . BucketUri } { storeKey } ?{ styleSegment } OSSAccessKeyId={ _requestContext . OssCredential . AccessKeyId } &Expires={ seconds } &Signature={ WebUtility . UrlEncode ( sign ) } ";
255+
256+ return url ;
257+ }
258+
259+ /// <summary>
260+ /// 生成直接post到oss的签名
261+ /// </summary>
262+ /// <param name="policy"></param>
263+ /// <returns></returns>
264+ public string ComputePostSignature ( string policy )
265+ {
266+ string sign = ServiceSignature . Create ( ) . ComputeSignature (
267+ _requestContext . OssCredential . AccessKeySecret , policy ) ;
268+
269+ return sign ;
270+ }
205271 }
206272}
0 commit comments