110 lines
4.0 KiB
C#
110 lines
4.0 KiB
C#
using Aliyun.OSS;
|
|
|
|
using CloudGaming.AppConfigModel;
|
|
|
|
using NPOI.HPSF;
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Security.AccessControl;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace CloudGaming.Code.Aliyun
|
|
{
|
|
/// <summary>
|
|
/// 阿里云扩展
|
|
/// </summary>
|
|
public static class AliyunConfigExtend
|
|
{
|
|
private static readonly Dictionary<string, string> MimeTypes = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase)
|
|
{
|
|
{ ".txt", "text/plain" },
|
|
{ ".html", "text/html" },
|
|
{ ".htm", "text/html" },
|
|
{ ".css", "text/css" },
|
|
{ ".js", "application/javascript" },
|
|
{ ".json", "application/json" },
|
|
{ ".xml", "application/xml" },
|
|
{ ".jpg", "image/jpeg" },
|
|
{ ".jpeg", "image/jpeg" },
|
|
{ ".png", "image/png" },
|
|
{ ".gif", "image/gif" },
|
|
{ ".bmp", "image/bmp" },
|
|
{ ".webp", "image/webp" },
|
|
{ ".svg", "image/svg+xml" },
|
|
{ ".pdf", "application/pdf" },
|
|
{ ".doc", "application/msword" },
|
|
{ ".docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document" },
|
|
{ ".xls", "application/vnd.ms-excel" },
|
|
{ ".xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" },
|
|
{ ".ppt", "application/vnd.ms-powerpoint" },
|
|
{ ".pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation" },
|
|
{ ".zip", "application/zip" },
|
|
{ ".rar", "application/x-rar-compressed" },
|
|
{ ".7z", "application/x-7z-compressed" },
|
|
{ ".mp3", "audio/mpeg" },
|
|
{ ".wav", "audio/wav" },
|
|
{ ".mp4", "video/mp4" },
|
|
{ ".avi", "video/x-msvideo" },
|
|
{ ".mov", "video/quicktime" }
|
|
};
|
|
public static string GetContentType(string fileName)
|
|
{
|
|
if (string.IsNullOrEmpty(fileName))
|
|
throw new ArgumentException("文件名称不能为空", nameof(fileName));
|
|
|
|
string extension = System.IO.Path.GetExtension(fileName);
|
|
|
|
if (MimeTypes.TryGetValue(extension, out string contentType))
|
|
{
|
|
return contentType;
|
|
}
|
|
|
|
return "application/octet-stream"; // 默认值
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取上传地址
|
|
/// </summary>
|
|
/// <param name="aliyunOssConfig">配置</param>
|
|
/// <param name="objectName">上传图片名称</param>
|
|
/// <param name="contentType">上传类型</param>
|
|
/// <returns></returns>
|
|
public static string GeneratePresignedUri(this AliyunConfig aliyunOssConfig, string objectName)
|
|
{
|
|
|
|
var contentType = GetContentType(objectName);
|
|
|
|
return GeneratePresignedUri(aliyunOssConfig, objectName, contentType);
|
|
}
|
|
/// <summary>
|
|
/// 获取上传地址
|
|
/// </summary>
|
|
/// <param name="aliyunOssConfig">配置</param>
|
|
/// <param name="objectName">上传图片名称</param>
|
|
/// <param name="contentType">上传类型</param>
|
|
/// <returns></returns>
|
|
public static string GeneratePresignedUri(this AliyunConfig aliyunOssConfig, string objectName, string contentType)
|
|
{
|
|
if (aliyunOssConfig == null)
|
|
{
|
|
throw new NullReferenceException("AliyunOssConfig不能为空");
|
|
}
|
|
// 创建OSSClient实例
|
|
var ossClient = new OssClient(aliyunOssConfig.EndPoint, aliyunOssConfig.AccessKeyId, aliyunOssConfig.AccessKeySecret);
|
|
|
|
// 生成签名URL
|
|
var generatePresignedUriRequest = new GeneratePresignedUriRequest(aliyunOssConfig.BucketName, objectName, SignHttpMethod.Put)
|
|
{
|
|
Expiration = DateTime.Now.AddHours(1),
|
|
ContentType = contentType
|
|
};
|
|
var signedUrl = ossClient.GeneratePresignedUri(generatePresignedUriRequest);
|
|
|
|
return signedUrl.ToString();
|
|
}
|
|
}
|
|
}
|