MinIO (五) .net core实现分片上传

开发环境

Win11

vs2022

appsettings.json添加配置项

  //minIO配置
  "MinIO": {
    //服务器IP
    "Endpoint": "192.168.xx.xx:9090",
    //账号
    "AccessKey": "3xR7i4zs1vLnxxxxxxxx",
    //密码
    "SecretKey": "P6bAnyzJm47Ub4WsIaz3TB4MCxbF5Bk2xxxxxxxx",
    //默认存储桶
    "Bucket": "demo",
    //保存文件的根目录
    "BucketDirectory": "c:\\aaa\\bbb\\ccc",
    //服务的URL地址
    "ServiceURL": "http://192.168.xx.xx:9090" //不可以用localhost,要用IP,比服务器IP多了一个http前缀
  }

注入代码

[ApiController]
[Route("api/[controller]/[action]")]
//[Authorize]
public class MinIOController : ControllerBase
{
    private static string _bucketName = string.Empty;//默认桶
    private static string _accessKey = string.Empty;
    private static string _secretKey = string.Empty;
    private readonly MinioClient _client;
    private readonly IConfiguration _configuration;
    private readonly IHttpClientFactory _httpClient;

    public MinIOController(MinioClient client,
        IConfiguration configuration,
        IHttpClientFactory httpClient)
    {
        _client = client;
        _configuration = configuration;
        _accessKey = configuration["MinIO:AccessKey"];
        _secretKey = configuration["MinIO:SecretKey"];
        _bucketName = configuration["MinIO:Bucket"];
        _httpClient = httpClient;
    }
    
    //。。。。。。

}

接下来是后端进行分片上传的代码示例,有两个,第一个是从官方考下来的代码示例,第二个是自己整理后的代码示例。

从官方考下来的代码示例

#region 后端进行分片上传示例(从官方文档上考下来的)
/// 
/// 后端进行分片上传示例(从官方文档上考下来的)
/// 
/// 文件流
/// 
[HttpPost]
public virtual async Task UploadPartAsync(IFormFile formFile)
{
    AmazonS3Config config = new AmazonS3Config()
    {
        ServiceURL = _configuration["MinIO:ServiceURL"]  //"http://192.168.50.36:9090/",
        //ServiceURL = "https://folder.s3.amazonaws.com/",
        //RegionEndpoint = Amazon.RegionEndpoint.CNNorth1
    };

    int MB = (int)Math.Pow(2, 20);  //以兆(M)为单位

    // Create a client
    using AmazonS3Client amazonS3Client = new AmazonS3Client(_configuration["MinIO:AccessKey"], _configuration["MinIO:SecretKey"], config);

    // Define input stream
    Stream inputStream = formFile.OpenReadStream(); //Create13MBDataStream();

    // Initiate multipart upload
    InitiateMultipartUploadRequest initRequest = new InitiateMultipartUploadRequest
    {
        BucketName = "test",
        Key = "Item1"
    };
    //InitiateMultipartUploadResponse initResponse = client.InitiateMultipartUpload(initRequest);
    InitiateMultipartUploadResponse initResponse = await amazonS3Client.InitiateMultipartUploadAsync(initRequest);

    GetPreSignedUrlRequest urlRequest = new GetPreSignedUrlRequest()
    {
        BucketName = "test",
        Key = "Item1",
        PartNumber = 1,
        Expires = DateTime.Now.AddMinutes(1)
    };

    // Upload part 1
    UploadPartRequest uploadRequest = new UploadPartRequest
    {
        BucketName = "test",
        Key = "Item1",
        UploadId = initResponse.UploadId,
        PartNumber = 1,
        PartSize = 6 * MB,
        InputStream = inputStream
    };
    UploadPartResponse up1Response = await amazonS3Client.UploadPartAsync(uploadRequest);

    // Upload part 2
    uploadRequest = new UploadPartRequest
    {
        BucketName = "test",
        Key = "Item1",
        UploadId = initResponse.UploadId,
        PartNumber = 2,
        PartSize = 6 * MB,
        InputStream = inputStream
    };
    UploadPartResponse up2Response = await amazonS3Client.UploadPartAsync(uploadRequest);

    // Upload part 3
    uploadRequest = new UploadPartRequest
    {
        BucketName = "test",
        Key = "Item1",
        UploadId = initResponse.UploadId,
        PartNumber = 3,
        InputStream = inputStream
    };
    UploadPartResponse up3Response = await amazonS3Client.UploadPartAsync(uploadRequest);

    // List parts for current upload
    ListPartsRequest listPartRequest = new ListPartsRequest
    {
        BucketName = "test",
        Key = "Item1",
        UploadId = initResponse.UploadId
    };
    ListPartsResponse listPartResponse = await amazonS3Client.ListPartsAsync(listPartRequest);
    Debug.Assert(listPartResponse.Parts.Count == 3);

    // Complete the multipart upload  分片上传完后合并
    CompleteMultipartUploadRequest compRequest = new CompleteMultipartUploadRequest
    {
        BucketName = "test",
        Key = "Item1",
        UploadId = initResponse.UploadId,
        PartETags = new List
        {
            new PartETag { ETag = up1Response.ETag, PartNumber = 1 },  //ETag就是分片信息
            new PartETag { ETag = up2Response.ETag, PartNumber = 2 },
            new PartETag { ETag = up3Response.ETag, PartNumber = 3 }
        }
    };
    CompleteMultipartUploadResponse compResponse = await amazonS3Client.CompleteMultipartUploadAsync(compRequest);

    return Ok();
}
#endregion 后端进行分片上传示例

整理后的后端进行分片上传

#region 后端进行分片上传示例
/// 
/// 后端进行分片上传示例
/// 
/// 要上传的文件路径
/// 上传到MinIO的路径
/// 
[HttpPost]
public virtual async Task UploadPartTestAsync(string path, string key)
{
    try
    {
        AmazonS3Config config = new AmazonS3Config()
        {
            ServiceURL = _configuration["MinIO:ServiceURL"]  //"http://192.168.50.36:9090/",
            //ServiceURL = "https://folder.s3.amazonaws.com/",
            //RegionEndpoint = Amazon.RegionEndpoint.CNNorth1
        };

        int MB = (int)Math.Pow(2, 20);  //以兆(M)为单位(等同于1*1024*1024)
        List partETagList = new List();
        int partSize = 30 * MB;  //每片大小

        // Create a client
        using AmazonS3Client amazonS3Client = new AmazonS3Client(_configuration["MinIO:AccessKey"], _configuration["MinIO:SecretKey"], config);

        Stopwatch sw = new Stopwatch();
        sw.Start();
        //文件流
        //Stream inputStream = formFile.OpenReadStream(); //Create13MBDataStream();
        //var files = Request.Form.Files;
        using Stream inputStream = new FileStream(path, FileMode.OpenOrCreate);
        //await files[0].CopyToAsync(inputStream);

        // Initiate multipart upload
        InitiateMultipartUploadRequest initRequest = new InitiateMultipartUploadRequest
        {
            BucketName = _configuration["MinIO:Bucket"],
            Key = key
        };
        //初始化分片上传,得到UploadId
        //InitiateMultipartUploadResponse initResponse = client.InitiateMultipartUpload(initRequest);
        InitiateMultipartUploadResponse initResponse = await amazonS3Client.InitiateMultipartUploadAsync(initRequest);
        TimeSpan ts = sw.Elapsed;
        sw.Restart();

        //得到URL
        //GetPreSignedUrlRequest urlRequest = new GetPreSignedUrlRequest()
        //{
        //    BucketName = _configuration["MinIO:Bucket"],
        //    Key = key,
        //    PartNumber = 1,
        //    Expires = DateTime.Now.AddMinutes(1)
        //};

        //分片数
        //int partCount = (int)Math.Ceiling((decimal)formFile.Length / partSize);
        int partCount = (int)Math.Ceiling((decimal)inputStream.Length / partSize);
        var tasks = new List>();
        for (int i = 1; i <= partCount; i++)
        {
            UploadPartRequest uploadRequest = new UploadPartRequest();
            uploadRequest.BucketName = _configuration["MinIO:Bucket"];
            uploadRequest.Key = key;
            uploadRequest.UploadId = initResponse.UploadId;
            uploadRequest.PartNumber = i;
            uploadRequest.InputStream = inputStream;
            if (i != partCount)
            {
                uploadRequest.PartSize = partSize;
            }
            //进行分片上传
            UploadPartResponse up1Response = await amazonS3Client.UploadPartAsync(uploadRequest);
            //tasks.Add(Task.Run(async () => await amazonS3Client.UploadPartAsync(uploadRequest)));
            PartETag partETag = new PartETag { ETag = up1Response.ETag, PartNumber = i };
            partETagList.Add(partETag);
        }
        //UploadPartResponse[] resultArr = await Task.WhenAll(tasks.ToArray());
        //for (int i = 0; i < resultArr.Length; i++)
        //{
        //    PartETag partETag = new PartETag { ETag = resultArr[i].ETag, PartNumber = i };
        //    partETagList.Add(partETag);
        //}
        TimeSpan ts2 = sw.Elapsed;
        sw.Restart();

         List parts for current upload(列出当前上载的部件)
        //ListPartsRequest listPartRequest = new ListPartsRequest
        //{
        //    BucketName = _configuration["MinIO:Bucket"],
        //    Key = key,
        //    UploadId = initResponse.UploadId
        //};
        //ListPartsResponse listPartResponse = await amazonS3Client.ListPartsAsync(listPartRequest);
        //Debug.Assert(listPartResponse.Parts.Count == partCount);
        //TimeSpan ts3 = sw.Elapsed;
        //sw.Restart();

        // Complete the multipart upload  合并上传
        CompleteMultipartUploadRequest compRequest = new CompleteMultipartUploadRequest
        {
            BucketName = _configuration["MinIO:Bucket"],
            Key = key,
            UploadId = initResponse.UploadId,
            PartETags = partETagList
        };
        CompleteMultipartUploadResponse compResponse = await amazonS3Client.CompleteMultipartUploadAsync(compRequest);
        TimeSpan ts4 = sw.Elapsed;
        sw.Restart();

        return Ok(new { Success = true, Message = "" });
    }
    catch (Exception ex)
    {
        return Ok(new { Success = true, Message = ex.Message });
    }
}
#endregion 后端进行分片上传示例

你可能感兴趣的:(MinIO,.netcore)