ABP VNext使用BlobMinio会存在上传没问题,但是查看文件会是下载的问题
主要原因是Minio会把Content-Type设置成application/octet-stream
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-VYUzjTHw-1649841171403)
因此不能使用ABP Vnext中的BlobMinio,因为它没有给content参数
解决方案:自己写方法
using System;
using System.IO;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Minio;
using Minio.Exceptions;
using Volo.Abp;
using Volo.Abp.BlobStoring;
using Volo.Abp.BlobStoring.Minio;
using Volo.Abp.DependencyInjection;
namespace Titan.Blob
{
public class FileSystemBlobManager : ITransientDependency
{
private readonly IHostingEnvironment _env;
private readonly IConfigurationRoot _appConfiguration;
public FileSystemBlobManager( [NotNull] IHostingEnvironment env)
{
_appConfiguration = GetAppConfiguration(env.ContentRootPath);
}
private IConfigurationRoot GetAppConfiguration(string path)
{
var builder = new ConfigurationBuilder()
.SetBasePath(path)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
builder = builder.AddEnvironmentVariables();
return builder.Build();
}
public async Task<string> SaveFile(string name, Stream file,string contentType)
{
var endPoint = _appConfiguration["MinIo:EndPoint"];
var accessKey = _appConfiguration["MinIo:AccessKey"];
var secretKey = _appConfiguration["MinIo:SecretKey"];
var bucketName = _appConfiguration["MinIo:BucketName"];
try
{
var minio = new MinioClient(endPoint, accessKey, secretKey);
//判断桶(文件夹)是否存在
bool found = await minio.BucketExistsAsync(bucketName);
if (!found)
{
//新增桶(文件夹)
await minio.MakeBucketAsync(bucketName, "host");
}
//文件大小
var len = file.Length;
//上传文件到桶(文件夹).
await minio.PutObjectAsync(bucketName, name, file, len, contentType, null, null);
//返回url 7天到期
var url = await minio.PresignedGetObjectAsync(bucketName, name, 3600 * 24 * 7);
return url;
}
catch (MinioException ex)
{
throw new Exception(ex.Message);
}
}
}