|
17 | 17 |
|
18 | 18 | using System; |
19 | 19 | using System.Collections.Generic; |
20 | | -using System.ComponentModel; |
21 | 20 | using System.Diagnostics; |
22 | 21 | using System.Globalization; |
23 | 22 | using System.IO; |
24 | 23 | using System.Linq; |
25 | 24 | using System.Net; |
26 | 25 | using System.Net.Http; |
27 | | -using System.Reflection; |
28 | 26 | using System.Runtime.InteropServices; |
29 | 27 | using System.Security.Cryptography; |
30 | 28 | using System.Text; |
@@ -273,6 +271,23 @@ public static string GetRandomName(int length = 5) |
273 | 271 | return "minio-dotnet-example-" + result; |
274 | 272 | } |
275 | 273 |
|
| 274 | + internal static void generateRandomFile(string fileName) |
| 275 | + { |
| 276 | + using (var fs = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None, 4096, true)) |
| 277 | + { |
| 278 | + var fileSize = 3L * 1024 * 1024 * 1024; |
| 279 | + var segments = fileSize / 10000; |
| 280 | + var last_seg = fileSize % 10000; |
| 281 | + var br = new BinaryWriter(fs); |
| 282 | + |
| 283 | + for (long i = 0; i < segments; i++) |
| 284 | + br.Write(new byte[10000]); |
| 285 | + |
| 286 | + br.Write(new byte[last_seg]); |
| 287 | + br.Close(); |
| 288 | + } |
| 289 | + } |
| 290 | + |
276 | 291 | // Return true if running in Mint mode |
277 | 292 | public static bool IsMintEnv() |
278 | 293 | { |
@@ -4023,36 +4038,6 @@ internal static async Task CopyObject_Test7(MinioClient minio) |
4023 | 4038 | } |
4024 | 4039 | } |
4025 | 4040 |
|
4026 | | - public static void objPrint(object obj) |
4027 | | - { |
4028 | | - foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(obj)) |
4029 | | - { |
4030 | | - var name = descriptor.Name; |
4031 | | - var value = descriptor.GetValue(obj); |
4032 | | - Console.WriteLine("{0}={1}", name, value); |
4033 | | - } |
4034 | | - } |
4035 | | - |
4036 | | - public static void Print(object obj) |
4037 | | - { |
4038 | | - foreach (var prop in obj.GetType() |
4039 | | - .GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)) |
4040 | | - { |
4041 | | - var value = prop.GetValue(obj, new object[] { }); |
4042 | | - Console.WriteLine("{0} = {1}", prop.Name, value); |
4043 | | - } |
4044 | | - |
4045 | | - Console.WriteLine("DONE!\n\n"); |
4046 | | - } |
4047 | | - |
4048 | | - public static void printDict(Dictionary<string, string> d) |
4049 | | - { |
4050 | | - if (d != null) |
4051 | | - foreach (var kv in d) |
4052 | | - Console.WriteLine(" {0} = {1}", kv.Key, kv.Value); |
4053 | | - Console.WriteLine("DONE!\n\n"); |
4054 | | - } |
4055 | | - |
4056 | 4041 | internal static async Task CopyObject_Test8(MinioClient minio) |
4057 | 4042 | { |
4058 | 4043 | var startTime = DateTime.Now; |
@@ -4788,6 +4773,83 @@ internal static async Task GetObject_3_OffsetLength_Tests(MinioClient minio) |
4788 | 4773 | } |
4789 | 4774 | } |
4790 | 4775 |
|
| 4776 | + internal static async Task GetObject_AsyncCallback_Test1(MinioClient minio) |
| 4777 | + { |
| 4778 | + var startTime = DateTime.Now; |
| 4779 | + var bucketName = GetRandomName(15); |
| 4780 | + var objectName = GetRandomObjectName(10); |
| 4781 | + string contentType = null; |
| 4782 | + var fileName = GetRandomName(10); |
| 4783 | + var destFileName = GetRandomName(10); |
| 4784 | + var args = new Dictionary<string, string> |
| 4785 | + { |
| 4786 | + { "bucketName", bucketName }, |
| 4787 | + { "objectName", objectName }, |
| 4788 | + { "contentType", contentType } |
| 4789 | + }; |
| 4790 | + |
| 4791 | + try |
| 4792 | + { |
| 4793 | + // Create a large local file |
| 4794 | + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) generateRandomFile(fileName); |
| 4795 | + else Bash("truncate -s 2G " + fileName); |
| 4796 | + |
| 4797 | + // Create the bucket |
| 4798 | + await Setup_Test(minio, bucketName); |
| 4799 | + |
| 4800 | + using (var filestream = new FileStream(File.OpenHandle(fileName), FileAccess.Read)) |
| 4801 | + { |
| 4802 | + // Upload the large file, "fileName", into the bucket |
| 4803 | + var size = filestream.Length; |
| 4804 | + long file_read_size = 0; |
| 4805 | + var putObjectArgs = new PutObjectArgs() |
| 4806 | + .WithBucket(bucketName) |
| 4807 | + .WithObject(objectName) |
| 4808 | + .WithStreamData(filestream) |
| 4809 | + .WithObjectSize(filestream.Length) |
| 4810 | + .WithContentType(contentType); |
| 4811 | + |
| 4812 | + await minio.PutObjectAsync(putObjectArgs).ConfigureAwait(false); |
| 4813 | + |
| 4814 | + var callbackAsync = async delegate(Stream stream, CancellationToken cancellationToken) |
| 4815 | + { |
| 4816 | + using (var dest = new FileStream(destFileName, FileMode.Create, FileAccess.Write)) |
| 4817 | + { |
| 4818 | + await stream.CopyToAsync(dest); |
| 4819 | + } |
| 4820 | + }; |
| 4821 | + |
| 4822 | + var getObjectArgs = new GetObjectArgs() |
| 4823 | + .WithBucket(bucketName) |
| 4824 | + .WithObject(objectName) |
| 4825 | + .WithCallbackStream(async (stream, cancellationToken) => await callbackAsync(stream, default)); |
| 4826 | + |
| 4827 | + await minio.GetObjectAsync(getObjectArgs).ConfigureAwait(false); |
| 4828 | + var writtenInfo = new FileInfo(destFileName); |
| 4829 | + file_read_size = writtenInfo.Length; |
| 4830 | + Assert.AreEqual(size, file_read_size); |
| 4831 | + |
| 4832 | + new MintLogger("GetObject_LargeFile_Test0", getObjectSignature, |
| 4833 | + "Tests whether GetObject as stream works", |
| 4834 | + TestStatus.PASS, DateTime.Now - startTime, args: args).Log(); |
| 4835 | + } |
| 4836 | + } |
| 4837 | + catch (Exception ex) |
| 4838 | + { |
| 4839 | + new MintLogger("GetObject_LargeFile_Test0", getObjectSignature, "Tests whether GetObject as stream works", |
| 4840 | + TestStatus.FAIL, DateTime.Now - startTime, ex.Message, ex.ToString(), args: args).Log(); |
| 4841 | + throw; |
| 4842 | + } |
| 4843 | + finally |
| 4844 | + { |
| 4845 | + if (File.Exists(fileName)) |
| 4846 | + File.Delete(fileName); |
| 4847 | + if (File.Exists(destFileName)) |
| 4848 | + File.Delete(destFileName); |
| 4849 | + await TearDown(minio, bucketName); |
| 4850 | + } |
| 4851 | + } |
| 4852 | + |
4791 | 4853 | internal static async Task FGetObject_Test1(MinioClient minio) |
4792 | 4854 | { |
4793 | 4855 | var startTime = DateTime.Now; |
|
0 commit comments