105040 移除缓存文件
This commit is contained in:
parent
d00c73d658
commit
a8f1fab64c
|
|
@ -9,16 +9,16 @@ using static System.Runtime.InteropServices.JavaScript.JSType;
|
|||
|
||||
namespace LFlow.Base.Utils
|
||||
{
|
||||
public class MinIOService
|
||||
public class MinIOService
|
||||
{
|
||||
private readonly IMinioClient minioClient;
|
||||
private readonly BucketManager bucketManager;
|
||||
private static string bucketName = "kecheng";
|
||||
private static string bucketName = "testbucket";
|
||||
|
||||
public MinIOService(IMinioClient _minioClient)
|
||||
{
|
||||
minioClient = _minioClient;
|
||||
bucketManager = new BucketManager(_minioClient);
|
||||
bucketManager = new BucketManager(_minioClient);
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -26,7 +26,7 @@ namespace LFlow.Base.Utils
|
|||
{
|
||||
try
|
||||
{
|
||||
|
||||
|
||||
//logger.LogInformation("文件开始上传");
|
||||
string uploadsPath = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "uploads");
|
||||
if (!Directory.Exists(uploadsPath))
|
||||
|
|
@ -34,32 +34,32 @@ namespace LFlow.Base.Utils
|
|||
Directory.CreateDirectory(uploadsPath);
|
||||
}
|
||||
string fileName = file.FileName;
|
||||
string filePath = Path.Combine(uploadsPath, fileName);
|
||||
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileName);
|
||||
string fileExtension = Path.GetExtension(fileName);
|
||||
string newFileName = null;
|
||||
int counter = 1;
|
||||
while (File.Exists(filePath))
|
||||
{
|
||||
//重新命名文件
|
||||
string Name = $"{fileNameWithoutExtension}_{counter}{fileExtension}";
|
||||
filePath = Path.Combine(uploadsPath, Name);
|
||||
newFileName = Path.GetFileNameWithoutExtension(filePath);
|
||||
counter++;
|
||||
}
|
||||
if (newFileName == null)
|
||||
{
|
||||
newFileName = Path.GetFileNameWithoutExtension(fileName);
|
||||
}
|
||||
using (var stream = new FileStream(filePath, FileMode.Create))
|
||||
{
|
||||
file.CopyTo(stream);
|
||||
}
|
||||
// string filePath = Path.Combine(uploadsPath, fileName);
|
||||
// string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileName);
|
||||
// string fileExtension = Path.GetExtension(fileName);
|
||||
// string newFileName = null;
|
||||
// int counter = 1;
|
||||
// while (File.Exists(filePath))
|
||||
// {
|
||||
// //重新命名文件
|
||||
// string Name = $"{fileNameWithoutExtension}_{counter}{fileExtension}";
|
||||
// filePath = Path.Combine(uploadsPath, Name);
|
||||
// newFileName = Path.GetFileNameWithoutExtension(filePath);
|
||||
// counter++;
|
||||
// }
|
||||
// if (newFileName == null)
|
||||
// {
|
||||
// newFileName = Path.GetFileNameWithoutExtension(fileName);
|
||||
// }
|
||||
// using (var stream = new FileStream(filePath, FileMode.Create))
|
||||
// {
|
||||
// file.CopyTo(stream);
|
||||
// }
|
||||
|
||||
//AddFileInfo(filePath, Path.GetFileNameWithoutExtension(fileName), newFileName);
|
||||
var beArgs = new BucketExistsArgs()
|
||||
.WithBucket(bucketName);
|
||||
|
||||
|
||||
bool found = await minioClient.BucketExistsAsync(beArgs);
|
||||
if (!found)
|
||||
{
|
||||
|
|
@ -70,12 +70,15 @@ namespace LFlow.Base.Utils
|
|||
// 设置上传文件的对象名
|
||||
var objectName = Guid.NewGuid().ToString() + Path.GetExtension(file.FileName);
|
||||
var putObjectArgs = new PutObjectArgs()
|
||||
.WithBucket(bucketName)
|
||||
.WithObject(objectName)
|
||||
.WithFileName(filePath)
|
||||
.WithContentType(file.ContentType);
|
||||
await minioClient.PutObjectAsync(putObjectArgs).ConfigureAwait(false);
|
||||
Console.WriteLine($"文件 '{filePath}' 上传到 bucket '{bucketName}' 中,文件名为 '{objectName}'。");
|
||||
.WithBucket(bucketName)
|
||||
.WithObject(objectName)
|
||||
// .WithFileName(filePath)
|
||||
.WithStreamData(file.OpenReadStream())
|
||||
.WithObjectSize(file.Length)
|
||||
.WithContentType(file.ContentType);
|
||||
var response = await minioClient.PutObjectAsync(putObjectArgs);
|
||||
|
||||
Console.WriteLine($"文件 '{fileName}' 上传到 bucket '{bucketName}' 中,文件名为 '{objectName}'。");
|
||||
return objectName;
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
|
|
|||
Loading…
Reference in New Issue