33
33
using System . Threading . Tasks ;
34
34
using System . Web ;
35
35
using System . Xml ;
36
+ using ICSharpCode . SharpZipLib . Core ;
37
+ using ICSharpCode . SharpZipLib . Zip ;
36
38
using Microsoft . VisualStudio . TestTools . UnitTesting ;
37
39
using Minio . DataModel ;
38
40
using Minio . DataModel . ILM ;
@@ -2393,6 +2395,129 @@ internal static async Task ObjectRetentionAsync_Test1(MinioClient minio)
2393
2395
2394
2396
#endregion
2395
2397
2398
+ internal static MemoryStream CreateZipFile ( string prefix , int nFiles )
2399
+ {
2400
+ var outputMemStream = new MemoryStream ( ) ;
2401
+ var zipStream = new ZipOutputStream ( outputMemStream ) ;
2402
+
2403
+ zipStream . SetLevel ( 3 ) ; //0-9, 9 being the highest level of compression
2404
+ byte [ ] bytes = null ;
2405
+
2406
+ for ( var i = 0 ; i <= nFiles ; i ++ )
2407
+ {
2408
+ // Make one large, compressible file.
2409
+ if ( i == nFiles ) i = 1000000 ;
2410
+
2411
+ var fileName = prefix + "file-" + i + ".bin" ;
2412
+ Directory . CreateDirectory ( prefix ) ;
2413
+ var newEntry = new ZipEntry ( fileName ) ;
2414
+ newEntry . DateTime = DateTime . Now ;
2415
+ zipStream . PutNextEntry ( newEntry ) ;
2416
+
2417
+ bytes = rsg . GenerateStreamFromSeed ( i ) . ToArray ( ) ;
2418
+ var inStream = new MemoryStream ( bytes ) ;
2419
+ if ( i == 0 ) StreamUtils . Copy ( inStream , zipStream , new byte [ 128 ] ) ;
2420
+ else StreamUtils . Copy ( inStream , zipStream , new byte [ i * 128 ] ) ;
2421
+
2422
+ inStream . Close ( ) ;
2423
+ zipStream . CloseEntry ( ) ;
2424
+ }
2425
+
2426
+ zipStream . IsStreamOwner = false ; // False stops the Close also Closing the underlying stream.
2427
+ zipStream . Close ( ) ; // Must finish the ZipOutputStream before using outputMemStream.
2428
+
2429
+ outputMemStream . Position = 0 ;
2430
+ outputMemStream . Seek ( 0 , SeekOrigin . Begin ) ;
2431
+
2432
+ return outputMemStream ;
2433
+ }
2434
+
2435
+ internal static async Task GetObjectS3Zip_Test1 ( MinioClient minio )
2436
+ {
2437
+ var path = "test/small/" ;
2438
+ var startTime = DateTime . Now ;
2439
+ var bucketName = GetRandomName ( 15 ) ;
2440
+ var randomFileName = GetRandomName ( 15 ) + ".zip" ;
2441
+ var objectName = GetRandomObjectName ( 15 ) + ".zip" ;
2442
+
2443
+ var args = new Dictionary < string , string >
2444
+ {
2445
+ { "bucketName" , bucketName } ,
2446
+ { "objectName" , objectName }
2447
+ } ;
2448
+ try
2449
+ {
2450
+ await Setup_Test ( minio , bucketName ) ;
2451
+ const int nFiles = 500 ;
2452
+ var memStream = CreateZipFile ( path , nFiles ) ;
2453
+
2454
+ var putObjectArgs = new PutObjectArgs ( )
2455
+ . WithBucket ( bucketName )
2456
+ . WithObject ( objectName )
2457
+ . WithStreamData ( memStream )
2458
+ . WithObjectSize ( memStream . Length ) ;
2459
+ await minio . PutObjectAsync ( putObjectArgs ) . ConfigureAwait ( false ) ;
2460
+
2461
+ var getObjectArgs = new GetObjectArgs ( )
2462
+ . WithBucket ( bucketName )
2463
+ . WithFile ( randomFileName )
2464
+ . WithObject ( objectName ) ;
2465
+
2466
+ var resp = await minio . GetObjectAsync ( getObjectArgs ) . ConfigureAwait ( false ) ;
2467
+
2468
+ var statArgs = new StatObjectArgs ( )
2469
+ . WithBucket ( bucketName )
2470
+ . WithObject ( objectName ) ;
2471
+ var stat = await minio . StatObjectAsync ( statArgs ) . ConfigureAwait ( false ) ;
2472
+
2473
+ var lOpts = new Dictionary < string , string > ( ) ;
2474
+ lOpts . Add ( "x-minio-extract" , "true" ) ;
2475
+
2476
+ // Test with different prefix values
2477
+ // prefix value="", expected number of files listed=1
2478
+ var prefix = "" ;
2479
+ ListObjects_Test ( minio , bucketName , prefix , 1 , true , headers : lOpts ) ;
2480
+
2481
+ // prefix value="/", expected number of files listed=nFiles+1
2482
+ prefix = objectName + "/" ;
2483
+ ListObjects_Test ( minio , bucketName , prefix , nFiles + 1 , true , headers : lOpts ) ;
2484
+
2485
+ // prefix value="/test", expected number of files listed=nFiles + 1
2486
+ prefix = objectName + "/test" ;
2487
+ ListObjects_Test ( minio , bucketName , prefix , nFiles + 1 , true , headers : lOpts ) ;
2488
+
2489
+ // prefix value="/test/", expected number of files listed=nFiles+1
2490
+ prefix = objectName + "/test/" ;
2491
+ ListObjects_Test ( minio , bucketName , prefix , nFiles + 1 , true , headers : lOpts ) ;
2492
+
2493
+ // prefix value="/test", expected number of files listed=nFiles+1
2494
+ prefix = objectName + "/test/small" ;
2495
+ ListObjects_Test ( minio , bucketName , prefix , nFiles + 1 , true , headers : lOpts ) ;
2496
+
2497
+ // prefix value="/test", expected number of files listed=nFiles+1
2498
+ prefix = objectName + "/test/small/" ;
2499
+ ListObjects_Test ( minio , bucketName , prefix , nFiles + 1 , true , headers : lOpts ) ;
2500
+
2501
+ // prefix value="/test", expected number of files listed=1
2502
+ prefix = objectName + "/test/small/" + "file-1.bin" ;
2503
+ ListObjects_Test ( minio , bucketName , prefix , 1 , true , headers : lOpts ) ;
2504
+
2505
+ new MintLogger ( "GetObjectS3Zip_Test1" , getObjectSignature , "Tests s3Zip files" , TestStatus . PASS ,
2506
+ DateTime . Now - startTime , args : args ) . Log ( ) ;
2507
+ }
2508
+ catch ( Exception ex )
2509
+ {
2510
+ new MintLogger ( "GetObjectS3Zip_Test1" , getObjectSignature , "Tests s3Zip files" , TestStatus . FAIL ,
2511
+ DateTime . Now - startTime , ex . Message , ex . ToString ( ) , args : args ) . Log ( ) ;
2512
+ throw ;
2513
+ }
2514
+ finally
2515
+ {
2516
+ File . Delete ( randomFileName ) ;
2517
+ Directory . Delete ( path . Split ( "/" ) [ 0 ] , true ) ;
2518
+ await TearDown ( minio , bucketName ) ;
2519
+ }
2520
+ }
2396
2521
2397
2522
#region Bucket Notifications
2398
2523
@@ -4976,13 +5101,14 @@ internal static async Task ListObjectVersions_Test1(MinioClient minio)
4976
5101
4977
5102
4978
5103
internal static void ListObjects_Test ( MinioClient minio , string bucketName , string prefix , int numObjects ,
4979
- bool recursive = true , bool versions = false )
5104
+ bool recursive = true , bool versions = false , Dictionary < string , string > headers = null )
4980
5105
{
4981
5106
var startTime = DateTime . Now ;
4982
5107
var count = 0 ;
4983
5108
var args = new ListObjectsArgs ( )
4984
5109
. WithBucket ( bucketName )
4985
5110
. WithPrefix ( prefix )
5111
+ . WithHeaders ( headers )
4986
5112
. WithRecursive ( recursive )
4987
5113
. WithVersions ( versions ) ;
4988
5114
if ( ! versions )
@@ -4991,11 +5117,11 @@ internal static void ListObjects_Test(MinioClient minio, string bucketName, stri
4991
5117
var subscription = observable . Subscribe (
4992
5118
item =>
4993
5119
{
4994
- Assert . IsTrue ( item . Key . StartsWith ( prefix ) ) ;
4995
- count += 1 ;
5120
+ if ( ! string . IsNullOrEmpty ( prefix ) ) Assert . IsTrue ( item . Key . StartsWith ( prefix ) ) ;
5121
+ count ++ ;
4996
5122
} ,
4997
5123
ex => throw ex ,
4998
- ( ) => { Assert . AreEqual ( count , numObjects ) ; } ) ;
5124
+ ( ) => { ; } ) ;
4999
5125
}
5000
5126
else
5001
5127
{
@@ -5007,8 +5133,11 @@ internal static void ListObjects_Test(MinioClient minio, string bucketName, stri
5007
5133
count += 1 ;
5008
5134
} ,
5009
5135
ex => throw ex ,
5010
- ( ) => { Assert . AreEqual ( count , numObjects ) ; } ) ;
5136
+ ( ) => { ; } ) ;
5011
5137
}
5138
+
5139
+ Thread . Sleep ( 1000 ) ;
5140
+ Assert . AreEqual ( numObjects , count ) ;
5012
5141
}
5013
5142
5014
5143
#endregion
0 commit comments