// Put an object from a local stream into bucket public async static Task Run(MinioClient minio, string bucketName = "my-bucket-name", string objectName = "my-object-name", string fileName = "location-of-file") { try { Console.WriteLine("Running example for API: PutObjectAsync with Tags"); var tags = new Dictionary <string, string> { { "Test-TagKey", "Test-TagValue" } }; PutObjectArgs args = new PutObjectArgs() .WithBucket(bucketName) .WithObject(objectName) .WithContentType("application/octet-stream") .WithFileName(fileName) .WithTagging(Tagging.GetObjectTags(tags)); await minio.PutObjectAsync(args); Console.WriteLine($"Uploaded object {objectName} to bucket {bucketName}"); Console.WriteLine(); } catch (Exception e) { Console.WriteLine($"[Bucket] Exception: {e}"); } }
public async Task <bool> PutObjectAsync(string bucketName, string objectName, Stream data, CancellationToken cancellationToken = default) { if (string.IsNullOrEmpty(bucketName)) { throw new ArgumentNullException(nameof(bucketName)); } objectName = FormatObjectName(objectName); string contentType = "application/octet-stream"; if (data is FileStream fileStream) { string fileName = fileStream.Name; if (!string.IsNullOrEmpty(fileName)) { new FileExtensionContentTypeProvider().TryGetContentType(fileName, out contentType); } } else { new FileExtensionContentTypeProvider().TryGetContentType(objectName, out contentType); } if (string.IsNullOrEmpty(contentType)) { contentType = "application/octet-stream"; } PutObjectArgs args = new PutObjectArgs() .WithBucket(bucketName) .WithObject(objectName) .WithStreamData(data) .WithObjectSize(data.Length) .WithContentType(contentType); await _client.PutObjectAsync(args, cancellationToken); return(true); }
public async Task <bool> PutObjectAsync(string bucketName, string objectName, string filePath, CancellationToken cancellationToken = default) { if (string.IsNullOrEmpty(bucketName)) { throw new ArgumentNullException(nameof(bucketName)); } objectName = FormatObjectName(objectName); if (!File.Exists(filePath)) { throw new Exception("File not exist."); } string fileName = Path.GetFileName(filePath); string contentType = null; if (!new FileExtensionContentTypeProvider().TryGetContentType(fileName, out contentType)) { contentType = "application/octet-stream"; } PutObjectArgs args = new PutObjectArgs() .WithBucket(bucketName) .WithObject(objectName) .WithFileName(filePath) .WithContentType(contentType); await _client.PutObjectAsync(args, cancellationToken); return(true); }
public async Task PresignedGetObjectWithHeaders() { // todo how to test this with mock client. var client = new MinioClient() .WithEndpoint("play.min.io") .WithCredentials("Q3AM3UQ867SPQQA43P2F", "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG"); var bucket = "bucket"; var objectName = "object-name"; Dictionary <string, string> reqParams = new Dictionary <string, string> { { "Response-Content-Disposition", "attachment; filename=\"filename.jpg\"" }, }; var bktExistArgs = new BucketExistsArgs() .WithBucket(bucket); bool found = await client.BucketExistsAsync(bktExistArgs); if (!found) { var mkBktArgs = new MakeBucketArgs() .WithBucket(bucket); await client.MakeBucketAsync(mkBktArgs); } if (!await this.ObjectExistsAsync(client, bucket, objectName)) { var helloData = Encoding.UTF8.GetBytes("hello world"); var helloStream = new MemoryStream(); helloStream.Write(helloData); var PutObjectArgs = new PutObjectArgs() .WithBucket(bucket) .WithObject(objectName) .WithStreamData(helloStream) .WithObjectSize(helloData.Length); await client.PutObjectAsync(PutObjectArgs); } PresignedGetObjectArgs presignedGetObjectArgs = new PresignedGetObjectArgs() .WithBucket("bucket") .WithObject("object-name") .WithExpiry(3600) .WithHeaders(reqParams) .WithRequestDate(_requestDate); var signedUrl = client.PresignedGetObjectAsync(presignedGetObjectArgs); Assert.AreEqual( "http://play.min.io/bucket/object-name?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=Q3AM3UQ867SPQQA43P2F%2F20200501%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20200501T154533Z&X-Amz-Expires=3600&X-Amz-SignedHeaders=host&response-content-disposition=attachment%3B%20filename%3D%22filename.jpg%22&X-Amz-Signature=de66f04dd4ac35838b9e83d669f7b5a70b452c6468e2b4a9e9c29f42e7fa102d", signedUrl); }
public async Task ReuseTcpTest() { var bucket = "bucket"; var objectName = "object-name"; var bktExistArgs = new BucketExistsArgs() .WithBucket(bucket); bool found = await this.MinioClient.BucketExistsAsync(bktExistArgs); if (!found) { var mkBktArgs = new MakeBucketArgs() .WithBucket(bucket); await this.MinioClient.MakeBucketAsync(mkBktArgs); } if (!await this.ObjectExistsAsync(this.MinioClient, bucket, objectName)) { var helloData = Encoding.UTF8.GetBytes("hello world"); var helloStream = new MemoryStream(); helloStream.Write(helloData); helloStream.Seek(0, SeekOrigin.Begin); var putObjectArgs = new PutObjectArgs() .WithBucket(bucket) .WithObject(objectName) .WithStreamData(helloStream) .WithObjectSize(helloData.Length); await this.MinioClient.PutObjectAsync(putObjectArgs); } var length = await this.GetObjectLength(bucket, objectName); Assert.IsTrue(length > 0); for (int i = 0; i < 100; i++) { // sequential execution, produce one tcp connection, check by netstat -an | grep 9000 length = this.GetObjectLength(bucket, objectName).Result; Assert.IsTrue(length > 0); } Parallel.ForEach(Enumerable.Range(0, 500), new ParallelOptions { MaxDegreeOfParallelism = 8 }, i => { // concurrent execution, produce eight tcp connections. length = this.GetObjectLength(bucket, objectName).Result; Assert.IsTrue(length > 0); }); }
public async Task PresignedGetObject() { // todo how to test this with mock client. var client = new MinioClient() .WithEndpoint("play.min.io") .WithCredentials("Q3AM3UQ867SPQQA43P2F", "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG"); var bucket = "bucket"; var objectName = "object-name"; var bktExistArgs = new BucketExistsArgs() .WithBucket(bucket); bool found = await client.BucketExistsAsync(bktExistArgs); if (!found) { var mkBktArgs = new MakeBucketArgs() .WithBucket(bucket); await client.MakeBucketAsync(mkBktArgs); } if (!await this.ObjectExistsAsync(client, bucket, objectName)) { var helloData = Encoding.UTF8.GetBytes("hello world"); var helloStream = new MemoryStream(); helloStream.Write(helloData); helloStream.Seek(0, SeekOrigin.Begin); var PutObjectArgs = new PutObjectArgs() .WithBucket(bucket) .WithObject(objectName) .WithStreamData(helloStream) .WithObjectSize(helloData.Length); await client.PutObjectAsync(PutObjectArgs); } PresignedGetObjectArgs presignedGetObjectArgs = new PresignedGetObjectArgs() .WithBucket("bucket") .WithObject("object-name") .WithExpiry(3600) .WithRequestDate(_requestDate); var signedUrl = client.PresignedGetObjectAsync(presignedGetObjectArgs); Assert.AreEqual( "http://play.min.io/bucket/object-name?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=Q3AM3UQ867SPQQA43P2F%2F20200501%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20200501T154533Z&X-Amz-Expires=3600&X-Amz-SignedHeaders=host&X-Amz-Signature=d4202da690618f77142d6f0557c97839f0773b2c718082e745cd9b199aa6b28f", signedUrl); }
/// <summary> /// Task that uploads a file to a bucket /// </summary> /// <param name="minio"></param> /// <returns></returns> private static async Task Run(MinioClient minio) { // Make a new bucket called mymusic. var bucketName = "mymusic-folder"; //<==== change this var location = "us-east-1"; // Upload the zip file var objectName = "my-golden-oldies.mp3"; // The following is a source file that needs to be created in // your local filesystem. var filePath = "C:\\Users\\vagrant\\Downloads\\golden_oldies.mp3"; var contentType = "application/zip"; try { var bktExistArgs = new BucketExistsArgs() .WithBucket(bucketName); var found = await minio.BucketExistsAsync(bktExistArgs); if (!found) { var mkBktArgs = new MakeBucketArgs() .WithBucket(bucketName) .WithLocation(location); await minio.MakeBucketAsync(mkBktArgs); } var putObjectArgs = new PutObjectArgs() .WithBucket(bucketName) .WithObject(objectName) .WithFileName(filePath) .WithContentType(contentType); await minio.PutObjectAsync(putObjectArgs).ConfigureAwait(false); Console.WriteLine($"\nSuccessfully uploaded {objectName}\n"); } catch (Exception e) { Console.WriteLine(e); } // Added for Windows folks. Without it, the window, tests // run in, dissappears as soon as the test code completes. if (IsWindows()) { Console.ReadLine(); } }
public async Task <string> SaveAsync(string fileName, string folderName, Stream stream) { var pointIndex = fileName.LastIndexOf("."); var hash = Guid.NewGuid().ToString("N"); var objectName = fileName.Insert(pointIndex, $"_{hash}_"); PutObjectArgs putObjectArgs = new PutObjectArgs() .WithBucket(_bucketName) .WithObject(objectName) .WithStreamData(stream) .WithObjectSize(stream.Length) .WithContentType("application/octet-stream"); await _minio.PutObjectAsync(putObjectArgs); return(objectName); }
// Upload object to bucket from file public static async Task Run(MinioClient minio, string bucketName = "my-bucket-name", string objectName = "my-object-name", string fileName = "from where") { try { Console.WriteLine("Running example for API: PutObjectAsync with FileName"); var args = new PutObjectArgs() .WithBucket(bucketName) .WithObject(objectName) .WithContentType("application/octet-stream") .WithFileName(fileName); await minio.PutObjectAsync(args).ConfigureAwait(false); Console.WriteLine($"Uploaded object {objectName} to bucket {bucketName}"); Console.WriteLine(); } catch (Exception e) { Console.WriteLine($"[Bucket] Exception: {e}"); } }
/// <summary> /// Task that uploads a file to a bucket /// </summary> /// <param name="minio"></param> /// <returns></returns> private static async Task Run(MinioClient minio) { // Make a new bucket called mymusic. var bucketName = "mymusic-folder"; //<==== change this var location = "us-east-1"; // Upload the zip file var objectName = "my-golden-oldies.mp3"; var filePath = "C:\\Users\\vagrant\\Downloads\\golden_oldies.mp3"; var contentType = "application/zip"; try { var bktExistArgs = new BucketExistsArgs() .WithBucket(bucketName); bool found = await minio.BucketExistsAsync(bktExistArgs); if (!found) { var mkBktArgs = new MakeBucketArgs() .WithBucket(bucketName) .WithLocation(location); await minio.MakeBucketAsync(mkBktArgs); } PutObjectArgs putObjectArgs = new PutObjectArgs() .WithBucket(bucketName) .WithObject(objectName) .WithFileName(filePath) .WithContentType(contentType); await minio.PutObjectAsync(putObjectArgs).ConfigureAwait(false); Console.WriteLine($"\nSuccessfully uploaded {objectName}\n"); } catch (Exception e) { Console.WriteLine(e); } }
// Put an object from a local stream into bucket public async static Task Run(MinioClient minio, string bucketName = "my-bucket-name", string objectName = "my-object-name", string fileName = "location-of-file", ServerSideEncryption sse = null) { try { byte[] bs = File.ReadAllBytes(fileName); Console.WriteLine("Running example for API: PutObjectAsync"); using (MemoryStream filestream = new MemoryStream(bs)) { FileInfo fileInfo = new FileInfo(fileName); var metaData = new Dictionary <string, string> { { "Test-Metadata", "Test Test" } }; PutObjectArgs args = new PutObjectArgs() .WithBucket(bucketName) .WithObject(objectName) .WithStreamData(filestream) .WithObjectSize(filestream.Length) .WithContentType("application/octet-stream") .WithHeaders(metaData) .WithServerSideEncryption(sse); await minio.PutObjectAsync(args); } Console.WriteLine($"Uploaded object {objectName} to bucket {bucketName}"); Console.WriteLine(); } catch (Exception e) { Console.WriteLine($"[Bucket] Exception: {e}"); } }
// Get object in a bucket public async static Task Run(MinioClient minio, string bucketName = "my-bucket-name", string objectName = "my-object-name", string fileName = "my-file-name") { string newObjectName = "new" + objectName; try { Console.WriteLine("Running example for API: SelectObjectContentAsync"); StringBuilder csvString = new StringBuilder(); csvString.AppendLine("Employee,Manager,Group"); csvString.AppendLine("Employee4,Employee2,500"); csvString.AppendLine("Employee3,Employee1,500"); csvString.AppendLine("Employee1,,1000"); csvString.AppendLine("Employee5,Employee1,500"); csvString.AppendLine("Employee2,Employee1,800"); var csvBytes = Encoding.UTF8.GetBytes(csvString.ToString()); using (var stream = new MemoryStream(csvBytes)) { PutObjectArgs putObjectArgs = new PutObjectArgs() .WithBucket(bucketName) .WithObject(newObjectName) .WithStreamData(stream) .WithObjectSize(stream.Length); await minio.PutObjectAsync(putObjectArgs); } QueryExpressionType queryType = QueryExpressionType.SQL; string queryExpr = "select count(*) from s3object"; SelectObjectInputSerialization inputSerialization = new SelectObjectInputSerialization() { CompressionType = SelectCompressionType.NONE, CSV = new CSVInputOptions() { FileHeaderInfo = CSVFileHeaderInfo.None, RecordDelimiter = "\n", FieldDelimiter = ",", } }; SelectObjectOutputSerialization outputSerialization = new SelectObjectOutputSerialization() { CSV = new CSVOutputOptions() { RecordDelimiter = "\n", FieldDelimiter = ",", } }; SelectObjectContentArgs args = new SelectObjectContentArgs() .WithBucket(bucketName) .WithObject(newObjectName) .WithExpressionType(queryType) .WithQueryExpression(queryExpr) .WithInputSerialization(inputSerialization) .WithOutputSerialization(outputSerialization); SelectResponseStream resp = await minio.SelectObjectContentAsync(args); resp.Payload.CopyTo(Console.OpenStandardOutput()); Console.WriteLine("Bytes scanned:" + resp.Stats.BytesScanned); Console.WriteLine("Bytes returned:" + resp.Stats.BytesReturned); Console.WriteLine("Bytes processed:" + resp.Stats.BytesProcessed); if (resp.Progress != null) { Console.WriteLine("Progress :" + resp.Progress.BytesProcessed); } } catch (Exception e) { Console.WriteLine($"[Bucket] Exception: {e}"); } }