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