148 lines
4.9 KiB
C#
Raw Normal View History

2025-06-06 16:00:39 +08:00
using Aliyun.OSS;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using VOL.Core.Configuration;
namespace VOL.Core.Utilities
{
/// <summary>
/// 阿里云Oss
/// </summary>
public class ALiYunOss
{
/// <summary>
/// 上传
/// </summary>
/// <param name="isIncludeExtension">是否包含文件后缀名</param>
/// <returns></returns>
public static string Upload(IFormFile file, string filePath, string fileName, bool isIncludeExtension = false)
{
if (file == null || file.Length == 0)
{
throw new ArgumentNullException("上传文件为空");
}
var url = string.Empty;
if (!isIncludeExtension)
{
// 拼接文件后缀名
var extension = Path.GetExtension(file.FileName);
filePath += fileName + extension;
}
else
{
filePath += fileName;
}
var stream = new MemoryStream();
file.CopyTo(stream);
stream.Position = 0;
// 创建OSSClient实例
var client = new OssClient(
AppSetting.ALiYunOSS.Endpoint,
AppSetting.ALiYunOSS.AccessKeyId,
AppSetting.ALiYunOSS.SecretAccessKey);
try
{
// 上传文件
var result = client.PutObject(AppSetting.YueDongServerOSS.BucketName, filePath, stream);
var uri = client.GeneratePresignedUri(AppSetting.YueDongServerOSS.BucketName, filePath);
url = "https://" + uri.Host + uri.LocalPath;
if (result.HttpStatusCode != HttpStatusCode.OK)
throw new Exception("上传失败");
}
catch (Exception ex)
{
throw new Exception("上传失败!");
}
finally
{
stream.Dispose();
}
return url;
}
/// <summary>
/// 获取指定前缀目录下的所有文件列表
/// </summary>
/// <param name="prefix">目录前缀,例如"student/"</param>
/// <returns>文件键列表</returns>
public static Dictionary<string, string> GetFileFromOss(string prefix)
{
var client = new OssClient(
AppSetting.ALiYunOSS.Endpoint,
AppSetting.ALiYunOSS.AccessKeyId,
AppSetting.ALiYunOSS.SecretAccessKey);
var listObjectsRequest = new ListObjectsRequest(AppSetting.YueDongServerOSS.BucketName)
{
Prefix = prefix
};
var result = client.ListObjects(listObjectsRequest);
Dictionary<string, string> imageDataDict = new Dictionary<string, string>();
foreach (var obj in result.ObjectSummaries)
{
// Fetch the object data from OSS
var objectData = client.GetObject(AppSetting.YueDongServerOSS.BucketName, obj.Key);
using (var memoryStream = new MemoryStream())
{
objectData.Content.CopyTo(memoryStream);
byte[] imageBytes = memoryStream.ToArray();
string base64Image = Convert.ToBase64String(imageBytes);
imageDataDict.Add(obj.Key, base64Image); // Store the file name and base64 data
}
}
return imageDataDict;
}
/// <summary>
/// 根据前缀删除指定路径下的所有文件
/// </summary>
/// <param name="prefix">目录前缀,例如"student/"</param>
public static void DeleteFilesByPrefix(string prefix)
{
var client = new OssClient(
AppSetting.ALiYunOSS.Endpoint,
AppSetting.ALiYunOSS.AccessKeyId,
AppSetting.ALiYunOSS.SecretAccessKey);
// 列出指定前缀的所有文件
var listObjectsRequest = new ListObjectsRequest(AppSetting.YueDongServerOSS.BucketName)
{
Prefix = prefix
};
var result = client.ListObjects(listObjectsRequest);
// 如果没有找到文件,直接返回
if (result.ObjectSummaries.Count() == 0)
{
return;
}
// 删除所有文件
foreach (var obj in result.ObjectSummaries)
{
try
{
// 删除文件
client.DeleteObject(AppSetting.YueDongServerOSS.BucketName, obj.Key);
}
catch (Exception ex)
{
throw new Exception("删除失败!");
}
}
}
}
}