GIAO DIỆN BLOG ĐƯỢC CHỈNH SỬA BỞI HUNG.PRO.VN

[CSHARP] How to download file from Minio server winform

[C#] How to download file from Minio server winform
Chào mọi người, hôm nay mình chia sẽ một chương trình khá thú vụ trong việc tải dữ liệu nâng cao từ trang web Minio

Và Minio là gì ?

MinIO (minio) là một server lưu trữ đối tượng dạng phân tán với hiệu năng cao.

Trình bày dễ hiểu thì MinIO là một file server giúp bạn dễ dàng upload file, download file như amazon, google drive, mediafire

Đặc biệt là MinIO cung cấp các api làm việc giống như Amazon S3, do đó bạn có thể upload, download file, lấy link… qua api một cách đơn giản mà không phải tự cài đặt. Đồng thời cũng rất tiện cho những bạn đang làm việc với Amazon Service.

Với Minio chúng ta có thể cấu hình kích thước file, loại file, các link public … và rất nhiều tiện ích khác.

DOWNLOAD FILE FROM MINIO C#

Đẩu tiên, để download được file từ MinIO, bạn cần các thông tin sau:
  1. Serect Key
  2. Access Key
Hai key này giúp chúng ta chứng thực vào server Minio để tải file về.

Giao diện demo ứng dụng C#:


Đầu tiên các bạn cài thư viện Minio từ Nuget
PM> NuGetInstall-Package Minio -Version 6.0.3

Full Code C#:

using Minio.DataModel.Args;
using Minio;
using Minio.Exceptions;
using System.Diagnostics;

namespace MiniIODemoDownload
{
    public partial class Form1 : Form
    {
        private string endpoint = "10.100.152.88:9000";
        private string accessKey = "xxxxx";
        private string secretKey = "xxxxxx";
        private string PATH_SAVE_MINIO_FILE = Path.Combine(Application.StartupPath, "minio_file");

        public Form1()
        {
            InitializeComponent();

            if (!Directory.Exists(PATH_SAVE_MINIO_FILE))
            {   
                Directory.CreateDirectory(PATH_SAVE_MINIO_FILE);
            }
     
        }
    
     
        public async Task<string> DownloadFileFromMinioAsync(string minioURL)
        {
            try
            {
                var parts = minioURL.Split(new[] { '#' }, 3);
                if (parts.Length < 3)
                {
                   MessageBox.Show("Đường dẫn minioURL không hợp lệ.");
                }

                var bucketPart = parts[2].Split('\')[0];
                var bucketName = bucketPart.Replace(":", "");

                var objectPart = parts[2].Replace(@"", "/");

                var objectName = objectPart.Substring(objectPart.IndexOf('/') + 1);


                var minioClient = new MinioClient()
                                    .WithEndpoint(endpoint)
                                    .WithCredentials(accessKey, secretKey)
                                    .WithSSL(false)
                                    .WithTimeout(3000)
                                    .Build();


                var localPath = $@"{PATH_SAVE_MINIO_FILE}{Path.GetFileName(minioURL)}";
                var result = await minioClient.GetObjectAsync(new GetObjectArgs()
                                                 .WithBucket(bucketName)
                                                 .WithObject(objectName)
                                                 .WithCallbackStream(stream =>
                                                 {
                                                     using (var fileStream = File.Create(localPath))
                                                     {
                                                         stream.CopyTo(fileStream);
                                                     }
                                                 })
                                                // .WithFile(localPath)
                                                 );

             

                if (result.Size > 0) {
                    return localPath;
                }
                
                return string.Empty;
               
            }
            catch (MinioException e)
            {
                Console.WriteLine($"File download failed: {e.Message}");
                return string.Empty;
            }
        }

        private async void btn_download_Click(object sender, EventArgs e)
        {
          
            string minioPath = textBox1.Text.Trim();
            var downloadedUri = await DownloadFileFromMinioAsync(minioPath);

            if (!string.IsNullOrEmpty(downloadedUri))
            {
                Process.Start(downloadedUri.ToString());
                MessageBox.Show($"File tải thành công: {downloadedUri}");
            }
            else
            {
                MessageBox.Show($"Tải file thất bại");
            }

           
        }

        
    }
}

Như vậy là chúng ta đã hoàn thành tiện ích Download Form Minio rồi nhé.
Chúc mọi người thành công.

DOWNLOAD SOURCE CODE MINIO


PassWord Unzip: HUNG.PRO.VN

Đăng nhận xét