C#结合Minio实现文件上传存储与更新
目录
- 前言
- 1.建立Minio链接实例
- 2.异步上传文件
- 3.下载文件
前言
MinIO是一个开源的对象存储服务器,专门设计用于在大规模数据存储环境中运行。它兼容Amazon S3 API,因此可以作为一个替代方案用于存储和检索数据,特别是用于云存储和分布式存储场景。
MinIO的部署非常简单,在官网下载 Minio.exe,然后使用批处理命令即可部署,非常方便。
C:/Users/admin/Desktop/minio.exe server Z:/Minio/Data/
后面的 Z:/Minio/Data/ 代表Minio存储的物理位置。
如果需要在C#中对Minio库进行操作,需要引入对应的Minio库。
官方的示例代码在github上已经有了,建议在官网Example中查找对应需要。
这里介绍几个常用的Minio在C#中的操作方法
1.建立Minio链接实例
public bool getMinoConnection(string IP) { // "KW5xQbKfmJ787YLGescu", "ANB72EgBqNGTdTGKE8KOad0DbuJ1sZtGCLE81234" 192.168.50.196 // "2MX2wsBGWIklR9EFdamG", "1234T0oLarccqIqc7ef406KWhTT0EkqreaAzELtY" 192.168.10.211 try { if (IP == "192.168.50.196" || IP == "192.168.50.195") { minioClient = new MinioClient().WithEndpoint("192.168.50.196", 9000) .WithCredentials("KW5xQbKfmJ787YLGescu", "ANB72EgBqNGTdTGKE8KOad0DbuJ1sZtGCLE81234") .Build(); } else if (IP == "192.168.10.211" || IP == "192.168.10.212") { minioClient = new MinioClient().WithEndpoint("192.168.10.211", 9000) .WithCredentials("2MX2wsBGWIklR9EFdamG", "1234T0oLarccqIqc7ef406KWhTT0EkqreaAzELtY") .Build(); } } catch (Exception ex) { Cojsnsole.Out.WriteLine(ex.Message); return false; } return true; }
2.异步上传文件
①.主要使用流的方式上传文件,内容以字符串为例
需要注意的是,如果上传的文件对应的MinioPath地址相同,则文件会直接覆盖,达到更新的作用
public async Task<bool> UploaderAsync(string IP, string MinioPath, string result) { if (getMinoConnection(IP)) { await Run(minioClient, MinioPath, result); minioClient.Dispose(); return true; } return false; } private async Task Run(IMinioClient minio, string MinioPath,string result) { var bucketName = "imagestore"; var objectName =www.devze.com MinioPath; var contentType = "application/text"; byte[] byteArray = Encoding.UTF8.GetBytes(result); try { using (MemoryStream stream = new MemoryStream(byteArray)) { stream.Position = 0; // Upload a file to bucket. var putObjectArgs = new PutObjectArgs() .WithBucket(bucketName) .WithObject(objectName) .WithObjectSize(stream.Length) .WithStreamData(stream); galYteH await minio.PutObjectAsync(putObjectArgs); } } catch (MinioException e) { Console.WriteLine("File Upload Error: {0}", e.Message); } }
②.使用本地文件的方式上传文件
private async Task Run(IMinioClient minio, string FolderName, string FileName, string BaseName) { var bucketName = "imagestore"; var objectName = FolderName + "/" + FileName; var filePath = BaseName; //var contentType = "application/jpg"; try { // Upload a file to bucket. var putObjectArgs = new PutObjectArgs() .WithBucket(bucketName) .WithObject(objectName) .WithFileName(filePath); //.WithContentType(contentType); await minio.PutObjectAsync(putObjectArgs) ; minio.Dispose(); } catch (MinioException e) { Console.WriteLine("File Upload Error: {0}", e.Message); } }
3.下载文件
我这里使用的http请求,你也可以按照官方库 GetObject去获取
public async Task<BitmapImage> DoGetFile(string minioheader, int ImgNums, string Ppath) { var MinioPath = Ppath.Split('/')[2] + "/" + Ppath.Split('/')[3] + "/images/" + ImgNums.ToString() + "_output_image.jpg"; BitmapImage bitmapImage = new BitmapImage(); using (HttpClient client = new HttpClient()) { try { HttpResponseMessage responseMessage = await client.GetAsync(minioheader + MinioPath); responseMessage.EnsureSuccessStatusCode(); byte[] imageBytes = await responseMessage.Content.ReadAsByteArrayAsync(); using (MemoryStream memory = new MemoryStream(imageBytes)) { bitmapImage.BeginInit(); bitmapImage.StreamSource = memory; bitmapImagejavascript.CacheOpti编程客栈on = BitmapCacheOption.OnLoad; bitmapImage.EndInit(); } } catch (Exception ex) { Console.WriteLine(ex.Message); } } return bitmapImage; }
差不多就这些
以上就是C#结合Minio实现文件上传存储与更新的详细内容,更多关于C# Minio文件上传存储与更新的资料请关注编程客栈(www.devze.com)其它相关文章!
精彩评论