17
17
using Cuiliang . AliyunOssSdk . Entites ;
18
18
using Cuiliang . AliyunOssSdk . Request ;
19
19
using Cuiliang . AliyunOssSdk . Utility ;
20
+ using Cuiliang . AliyunOssSdk . Utility . Authentication ;
20
21
21
22
namespace Cuiliang . AliyunOssSdk
22
23
{
@@ -51,9 +52,9 @@ public async Task<OssResult<ListBucketsResult>> ListBucketsAsync(string region)
51
52
/// <param name="key"></param>
52
53
/// <param name="file"></param>
53
54
/// <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 )
55
56
{
56
- var cmd = new PutObjectCommand ( _requestContext , bucket , key , file , null ) ;
57
+ var cmd = new PutObjectCommand ( _requestContext , bucket , key , file , extraHeaders ) ;
57
58
58
59
return await cmd . ExecuteAsync ( _client ) ;
59
60
}
@@ -66,16 +67,17 @@ public async Task<OssResult<PutObjectResult>> PutObjectAsync(BucketInfo bucket,
66
67
/// <param name="content"></param>
67
68
/// <param name="mimeType"></param>
68
69
/// <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 )
70
71
{
71
72
var file = new RequestContent ( )
72
73
{
73
74
ContentType = RequestContentType . String ,
74
75
StringContent = content ,
75
- MimeType = mimeType
76
+ MimeType = mimeType ,
77
+ Metadata = meta
76
78
} ;
77
79
78
- return await PutObjectAsync ( bucket , key , file ) ;
80
+ return await PutObjectAsync ( bucket , key , file , extraHeaders ) ;
79
81
}
80
82
81
83
/// <summary>
@@ -86,21 +88,45 @@ public async Task<OssResult<PutObjectResult>> PutObjectAsync(BucketInfo bucket,
86
88
/// <param name="filePathName"></param>
87
89
/// <returns></returns>
88
90
public async Task < OssResult < PutObjectResult > > PutObjectByFileNameAsync ( BucketInfo bucket , string key ,
89
- string filePathName )
91
+ string filePathName , ObjectMetadata meta = null , IDictionary < string , string > extraHeaders = null )
90
92
{
91
93
using ( var stream = File . OpenRead ( filePathName ) )
92
94
{
93
95
var file = new RequestContent ( )
94
96
{
95
97
ContentType = RequestContentType . Stream ,
96
98
StreamContent = stream ,
97
- MimeType = MimeHelper . GetMime ( filePathName )
99
+ MimeType = MimeHelper . GetMime ( filePathName ) ,
100
+ Metadata = meta
98
101
} ;
99
102
100
- return await PutObjectAsync ( bucket , key , file ) ;
103
+ return await PutObjectAsync ( bucket , key , file , extraHeaders ) ;
101
104
}
102
105
}
103
106
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
+
104
130
/// <summary>
105
131
/// 复制对象
106
132
/// </summary>
@@ -202,5 +228,45 @@ public async Task<OssResult<GetObjectMetaResult>> GetObjectMetaAsync(BucketInfo
202
228
var cmd = new GetObjectMetaCommand ( _requestContext , bucket , key ) ;
203
229
return await cmd . ExecuteAsync ( _client ) ;
204
230
}
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
+ }
205
271
}
206
272
}
0 commit comments