【.net 6.0, .net core minio文件上传下载】

这里写自定义目录标题

  • 欢迎使用Markdown编辑器
    • 文件上传
    • 文件下载

欢迎使用Markdown编辑器

文件上传

昨晚IFormFile转MemoryStream转不过来,上传始终为0kb。当时懵了没解决掉。搜了搜流的转换,IFormFile的 OpenReadStream我也没走通。今天到公司看了一下,流没倒带,seek回begin就好了。在公司测试是.net core3.1,6.0试完了,也是一样的问题。

        [HttpPost,Route("uploadFile")]
        public async Task<object> UploadFile([FromForm] MinIOVO vo)
        {
            MinioClient minioClient = new MinioClient("ip:9000", "minioadmin", "minioadmin");
            bool isExsit = await minioClient.BucketExistsAsync("performance");
            if (!isExsit) return null;
            using (MemoryStream stream = new MemoryStream())
            {
                await vo.File.CopyToAsync(stream);
                stream.Seek(0, SeekOrigin.Begin);//看这的指针是否在0
                await minioClient.PutObjectAsync("performance",
                   vo.File.FileName,
                    stream,
                    stream.Length,
                   vo.File.ContentType);
                Console.Out.WriteLine("island.jpg is uploaded successfully");
            }
            return Ok("ok fine");
        }

【.net 6.0, .net core minio文件上传下载】_第1张图片

实体类

    public class MinIOVO
    {
        public IFormFile File { get; set; }
    }

文件下载

        [HttpPost, Route("putFileTest")]
        public async Task<object> PutFileTest([FromForm] FileVO vo)//不要在意这个参数,我给上传用的
        {

            try
            {
                var minioClient = new MinioClient("ip:9000",
                                       "minioadmin",//账号
                                       "minioadmin"//密码
                                 );

                bool isExsitBucket = await _minioClient.BucketExistsAsync("yeyu");
                if (!isExsitBucket) return null;
                MemoryStream stream1 = new MemoryStream();
                await minioClient.StatObjectAsync("yeyu", "1638195041(1).jpg");

                // Get input stream to have content of 'my-objectname' from 'my-bucketname'
                await minioClient.GetObjectAsync("yeyu", "1638195041(1).jpg",
                                                 (stream) =>
                                                 {
                                                     stream.CopyTo(stream1);
                                                 });
                stream1.Seek(0, SeekOrigin.Begin);//我建议你倒带
                return File(stream1, "image/jpeg");//这里文件类型建议存下来,方便下载时直接取用
            }
            catch (MinioException e)
            {
                Console.Out.WriteLine("Error occurred: " + e);
            }
            return null;
        }

minio官方文档

你可能感兴趣的:(C#,.net,microsoft)