后端批量上传修改
continuous-integration/drone/push Build is passing Details

Test_IRC_Net8
hang 2025-08-07 16:00:15 +08:00
parent 5956937476
commit 695de70894
1 changed files with 37 additions and 37 deletions

View File

@ -190,7 +190,7 @@ public class OSSService : IOSSService
/// <returns></returns>
public async Task<string> UploadToOSSAsync(Stream fileStream, string oosFolderPath, string fileRealName, bool isFileNameAddGuid = true)
{
GetObjectStoreTempToken();
BackBatchGetToken();
var ossRelativePath = isFileNameAddGuid ? $"{oosFolderPath}/{Guid.NewGuid()}_{fileRealName}" : $"{oosFolderPath}/{fileRealName}";
@ -280,6 +280,37 @@ public class OSSService : IOSSService
}
//后端批量上传 或者下载不每个文件获取临时token
private void BackBatchGetToken()
{
if (ObjectStoreServiceOptions.ObjectStoreUse == "AliyunOSS")
{
if(AliyunOSSTempToken == null)
{
GetObjectStoreTempToken();
}
//token 过期了
else if (AliyunOSSTempToken.Expiration.AddSeconds(10) <= DateTime.Now)
{
GetObjectStoreTempToken();
}
}
else if (ObjectStoreServiceOptions.ObjectStoreUse == "AWS")
{
if (AWSTempToken == null)
{
GetObjectStoreTempToken();
}
//token 过期了
else if (AWSTempToken.Expiration?.AddSeconds(10) <= DateTime.Now)
{
GetObjectStoreTempToken();
}
}
}
/// <summary>
/// oosFolderPath 不要 "/ "开头 应该: TempFolder/ChildFolder
@ -291,7 +322,7 @@ public class OSSService : IOSSService
/// <exception cref="BusinessValidationFailedException"></exception>
public async Task<string> UploadToOSSAsync(string localFilePath, string oosFolderPath, bool isFileNameAddGuid = true)
{
GetObjectStoreTempToken();
BackBatchGetToken();
var localFileName = Path.GetFileName(localFilePath);
@ -361,22 +392,7 @@ public class OSSService : IOSSService
public async Task DownLoadFromOSSAsync(string ossRelativePath, string localFilePath)
{
if (isFirstCall)
{
GetObjectStoreTempToken();
isFirstCall = false;
}
else
{
//判断是否过期了
if (ObjectStoreServiceOptions.ObjectStoreUse == "AliyunOSS")
{
if (AliyunOSSTempToken != null && AliyunOSSTempToken.Expiration <= DateTime.Now)
{
GetObjectStoreTempToken();
}
}
}
BackBatchGetToken();
ossRelativePath = ossRelativePath.TrimStart('/');
try
@ -394,7 +410,7 @@ public class OSSService : IOSSService
// 将下载的文件流保存到本地文件
using (var fs = File.OpenWrite(localFilePath))
{
await result.Content.CopyToAsync(fs);
await result.Content.CopyToAsync(fs);
}
}
@ -1014,25 +1030,9 @@ public class OSSService : IOSSService
}
}
private bool isFirstCall = true;
public async Task<long> GetObjectSizeAsync(string sourcePath)
{
if (isFirstCall)
{
GetObjectStoreTempToken();
isFirstCall = false;
}
else
{
//判断是否过期了
if (ObjectStoreServiceOptions.ObjectStoreUse == "AliyunOSS")
{
if(AliyunOSSTempToken != null && AliyunOSSTempToken.Expiration <= DateTime.Now)
{
GetObjectStoreTempToken();
}
}
}
BackBatchGetToken();
var objectkey = sourcePath.Trim('/');
@ -1046,7 +1046,7 @@ public class OSSService : IOSSService
var key = HttpUtility.UrlDecode(objectkey);
var metadata = _ossClient.GetObjectMetadata(aliConfig.BucketName, key);
long fileSize = metadata?.ContentLength??0; // 文件大小(字节)
long fileSize = metadata?.ContentLength ?? 0; // 文件大小(字节)
return fileSize;
}