@@ -144,6 +144,63 @@ public void UploadFileV2Test()
144144 System . IO . File . Delete ( filePath ) ;
145145 }
146146
147+ [ Test ]
148+ public void UploadFileV2WithoutKeyTest ( )
149+ {
150+ Mac mac = new Mac ( AccessKey , SecretKey ) ;
151+
152+ string tempPath = Path . GetTempPath ( ) ;
153+ int rnd = new Random ( ) . Next ( 1 , 100000 ) ;
154+ string filePath = tempPath + "resumeFile" + rnd . ToString ( ) ;
155+ char [ ] testBody = new char [ 6 * 1024 * 1024 ] ;
156+ FileStream stream = new FileStream ( filePath , FileMode . Create ) ;
157+ StreamWriter sw = new StreamWriter ( stream , System . Text . Encoding . Default ) ;
158+ sw . Write ( testBody ) ;
159+ sw . Close ( ) ;
160+ stream . Close ( ) ;
161+
162+ PutPolicy putPolicy = new PutPolicy ( ) ;
163+ putPolicy . Scope = Bucket ;
164+ putPolicy . SetExpires ( 3600 ) ;
165+ putPolicy . DeleteAfterDays = 1 ;
166+ string token = Auth . CreateUploadToken ( mac , putPolicy . ToJsonString ( ) ) ;
167+
168+ Config config = new Config ( ) ;
169+ config . Zone = Zone . ZONE_CN_East ;
170+ config . UseHttps = true ;
171+ config . UseCdnDomains = true ;
172+ config . ChunkSize = ChunkUnit . U512K ;
173+ PutExtra extra = new PutExtra ( ) ;
174+ extra . MimeType = "application/json" ;
175+ extra . Version = "v2" ;
176+ extra . PartSize = 4 * 1024 * 1024 ;
177+ ResumableUploader target = new ResumableUploader ( config ) ;
178+ HttpResult result = target . UploadFile ( filePath , null , token , extra ) ;
179+ Console . WriteLine ( "chunk upload result: " + result . ToString ( ) ) ;
180+ Assert . AreEqual ( ( int ) HttpCode . OK , result . Code ) ;
181+ Dictionary < string , string > responseBody = JsonConvert . DeserializeObject < Dictionary < string , string > > ( result . Text ) ;
182+ Assert . AreEqual ( responseBody [ "hash" ] , responseBody [ "key" ] ) ;
183+
184+ string downloadUrl = string . Format ( "http://{0}/{1}" , Domain , responseBody [ "key" ] ) ;
185+ HttpWebRequest wReq = WebRequest . Create ( downloadUrl ) as HttpWebRequest ;
186+ wReq . Method = "GET" ;
187+ HttpWebResponse wResp = wReq . GetResponse ( ) as HttpWebResponse ;
188+ Assert . AreEqual ( ( int ) HttpCode . OK , ( int ) wResp . StatusCode ) ;
189+ Assert . AreEqual ( "application/json" , wResp . Headers [ HttpResponseHeader . ContentType ] ) ;
190+
191+ using ( var md5_1 = MD5 . Create ( ) ) {
192+ using ( var md5_2 = MD5 . Create ( ) ) {
193+ using ( var fileStream = File . OpenRead ( filePath ) ) {
194+ byte [ ] checksum1 = md5_1 . ComputeHash ( fileStream ) ;
195+ byte [ ] checksum2 = md5_2 . ComputeHash ( wResp . GetResponseStream ( ) ) ;
196+ Assert . AreEqual ( checksum1 , checksum2 ) ;
197+ }
198+ }
199+ }
200+
201+ File . Delete ( filePath ) ;
202+ }
203+
147204 [ Test ]
148205 public void ResumeUploadFileTest ( )
149206 {
@@ -264,6 +321,71 @@ public void ResumeUploadFileV2Test()
264321 System . IO . File . Delete ( filePath ) ;
265322 }
266323 }
324+
325+ [ Test ]
326+ public void ResumeUploadFileV2WithoutKeyTest ( )
327+ {
328+ Mac mac = new Mac ( AccessKey , SecretKey ) ;
329+ Config config = new Config ( ) ;
330+ config . UseHttps = true ;
331+ config . Zone = Zone . ZONE_CN_East ;
332+ config . UseCdnDomains = true ;
333+ config . ChunkSize = ChunkUnit . U512K ;
334+ ResumableUploader target = new ResumableUploader ( config ) ;
335+ PutExtra extra = new PutExtra ( ) ;
336+ extra . PartSize = 4 * 1024 * 1024 ;
337+ extra . Version = "v2" ;
338+
339+ int [ ] sizes = new int [ 5 ] { extra . PartSize / 2 , extra . PartSize , extra . PartSize + 1 , extra . PartSize * 2 , 10 * 1024 * 1024 } ;
340+ foreach ( int i in sizes )
341+ {
342+ char [ ] testBody = new char [ i ] ;
343+ Random rand = new Random ( ) ;
344+
345+ string tempPath = Path . GetTempPath ( ) ;
346+ int rnd = new Random ( ) . Next ( 1 , 100000 ) ;
347+ string filePath = tempPath + "resumeFile" + rnd . ToString ( ) ;
348+ FileStream stream = new FileStream ( filePath , FileMode . Create ) ;
349+ StreamWriter sw = new StreamWriter ( stream , System . Text . Encoding . Default ) ;
350+ sw . Write ( testBody ) ;
351+ sw . Close ( ) ;
352+ stream . Close ( ) ;
353+ Stream fs = File . OpenRead ( filePath ) ;
354+
355+ PutPolicy putPolicy = new PutPolicy ( ) ;
356+ putPolicy . Scope = Bucket ;
357+ putPolicy . SetExpires ( 3600 ) ;
358+ putPolicy . DeleteAfterDays = 1 ;
359+ string token = Auth . CreateUploadToken ( mac , putPolicy . ToJsonString ( ) ) ;
360+
361+ //设置断点续传进度记录文件
362+ extra . ResumeRecordFile = ResumeHelper . GetDefaultRecordKey ( filePath , rand . Next ( ) . ToString ( ) ) ;
363+ Console . WriteLine ( "record file:" + extra . ResumeRecordFile ) ;
364+ HttpResult result = target . UploadStream ( fs , null , token , extra ) ;
365+ Console . WriteLine ( "resume upload: " + result . ToString ( ) ) ;
366+ Assert . AreEqual ( ( int ) HttpCode . OK , result . Code ) ;
367+ Dictionary < string , string > responseBody = JsonConvert . DeserializeObject < Dictionary < string , string > > ( result . Text ) ;
368+ Assert . AreEqual ( responseBody [ "hash" ] , responseBody [ "key" ] ) ;
369+
370+ string downloadUrl = string . Format ( "http://{0}/{1}" , Domain , responseBody [ "key" ] ) ;
371+ HttpWebRequest wReq = WebRequest . Create ( downloadUrl ) as HttpWebRequest ;
372+ wReq . Method = "GET" ;
373+ HttpWebResponse wResp = wReq . GetResponse ( ) as HttpWebResponse ;
374+ Assert . AreEqual ( ( int ) HttpCode . OK , ( int ) wResp . StatusCode ) ;
375+
376+ using ( var md5_1 = MD5 . Create ( ) ) {
377+ using ( var md5_2 = MD5 . Create ( ) ) {
378+ using ( var fileStream = File . OpenRead ( filePath ) ) {
379+ byte [ ] checksum1 = md5_1 . ComputeHash ( fileStream ) ;
380+ byte [ ] checksum2 = md5_2 . ComputeHash ( wResp . GetResponseStream ( ) ) ;
381+ Assert . AreEqual ( checksum1 , checksum2 ) ;
382+ }
383+ }
384+ }
385+
386+ File . Delete ( filePath ) ;
387+ }
388+ }
267389
268390 }
269391}
0 commit comments