增加上传华为云obs接口

This commit is contained in:
范露尧
2023-01-19 15:49:02 +08:00
parent 57129b3861
commit 21dd025ab8
10 changed files with 602 additions and 1 deletions

View File

@@ -14,6 +14,7 @@
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="5.0.5" />
<PackageReference Include="Microsoft.Data.SqlClient" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.0-preview.1.21102.12" />
<PackageReference Include="OnceMi.AspNetCore.OSS" Version="1.1.9" />
</ItemGroup>
<ItemGroup>

View File

@@ -8,6 +8,7 @@ using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using OnceMi.AspNetCore.OSS;
using Serilog;
using System.Text.Json;
using System.Threading.Tasks;
@@ -67,6 +68,7 @@ namespace Ewide.Web.Core
// 设置雪花id的workerId确保每个实例workerId都应不同
//var workerId = ushort.Parse(App.Configuration["SnowId:WorkerId"] ?? "1");
//IDGenerator.SetIdGenerator(new IDGeneratorOptions { WorkerId = workerId });
services.AddOSSService("HuaweiCloud", "OSSProvider");
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)

View File

@@ -16,6 +16,10 @@ namespace Ewide.Web.Entry
{
webBuilder.Inject()
.UseStartup<Startup>();
//.UseKestrel(options =>
//{
// options.Limits.MaxRequestBodySize = null; // 设置 null 就是不限制,具体值就是限制最大多少 M
//});
})
.UseSerilogDefault();
}

View File

@@ -33,7 +33,15 @@
"WithOrigins": [ "http://localhost:8080", "https://mapi.zjzwfw.gov.cn" ]
},
"AppSettings": {
"InjectSpecificationDocument": false
"InjectSpecificationDocument1": false
},
"OSSProvider": {
"Provider": "HuaweiCloud", //枚举值支持Minio/Aliyun/QCloud
"Endpoint": "http://10.74.25.87:6020", //腾讯云中表示AppId
"Region": "", //地域
"AccessKey": "C4D30C2801D928AAF687",
"SecretKey": "ooZVXaB1tqIz7DHTv53RILD7o5cAAAGAAdkoqlR2",
"IsEnableCache": true //是否启用缓存,推荐开启
}
}

View File

@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="1073741824" />
</requestFiltering>
</security>
</system.webServer>
</configuration>

View File

@@ -0,0 +1,278 @@
using Ewide.Core;
using Furion.DynamicApiController;
using Furion.VirtualFileServer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using NPOI.OpenXml4Net.OPC.Internal;
using OBS;
using OBS.Model;
using OnceMi.AspNetCore.OSS;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using Vote.Services.Dto;
namespace Vote.Services.ApiController
{
/// <summary>
/// OBS
/// </summary>
[ApiDescriptionSettings("huawei", Order = 0)]
[Route("/huawei")]
[AllowAnonymous]
public class HuaweiService : IDynamicApiController
{
private readonly IOSSService _OSSService;
private readonly string _bucketName;
private readonly string filePrefix;
/// <summary>
///
/// </summary>
/// <param name="OSSService"></param>
public HuaweiService(IOSSService OSSService)
{
_OSSService = OSSService;
_bucketName = "94.229";
filePrefix = "D:\\obsFile\\";
}
/// <summary>
/// 列出当前账号下允许访问的所有储存桶。
/// </summary>
/// <returns></returns>
public async Task<dynamic> ListBuckets()
{
try
{
var result = await _OSSService.ListBucketsAsync();
return result;
}
catch (Exception ex)
{
throw;
}
}
/// <summary>
/// 获取储存桶的外部访问权限。
/// </summary>
/// <returns></returns>
public async Task<dynamic> GetBucketAclAsync()
{
try
{
var result = await _OSSService.GetBucketAclAsync(_bucketName);
return result;
}
catch (Exception ex)
{
throw;
}
}
/// <summary>
/// 获取指定储存桶中指定对象是否存在。
/// </summary>
/// <returns></returns>
public async Task<dynamic> ObjectsExistsAsync([FromBody] GetObjectInput args)
{
if (args == null || string.IsNullOrWhiteSpace(args.objectName))
return await Display(false, "error");
try
{
var objectName = args.objectName;
if (objectName.ToLower().EndsWith(".dwg"))
{
objectName += ".zip";
}
var result = await _OSSService.ObjectsExistsAsync(_bucketName, objectName);
return result;
}
catch (Exception ex)
{
throw;
}
}
/// <summary>
/// 获取文件的数据流。 大小写敏感!
/// </summary>
/// <returns></returns>
[HttpPost]
public async Task<IActionResult> DownloadObjectAsync([FromBody] GetObjectInput args)
{
if (args == null || string.IsNullOrWhiteSpace(args.objectName))
return await Display(false, "error");
var objectName = args.objectName;
if (objectName.ToLower().EndsWith(".dwg"))
{
objectName += ".zip";
}
#region MyRegion
//FileStreamResult fileStreamResult = null;
//await _OSSService.GetObjectAsync(_bucketName, objectName, (stream) =>
//{
// if (FS.TryGetContentType(objectName, out var contentType) || GetContentType(objectName, out contentType))
// fileStreamResult = new FileStreamResult(stream, contentType) { FileDownloadName = objectName };
//});
//if (fileStreamResult != null)
// return fileStreamResult;
//else
// return await Display(false, "文件格式有问题");
//var localPath = filePrefix + _bucketName + '/' + args.objectName;
//await _OSSService.GetObjectAsync(_bucketName, args.objectName, localPath);
//if (FS.TryGetContentType(args.objectName, out var contentType) || GetContentType(args.objectName, out contentType))
//{
// return new FileStreamResult(new FileStream(localPath, FileMode.Open), contentType) { FileDownloadName = args.objectName };
//}
//else
//{
// return await Display(false, "文件格式有问题");
//}
#endregion
#region sdk
try
{
//var objectName = "/files/coc/202011/11/2020kdfj0075_5.pdf";
ObsClient client = new ObsClient(_OSSService.Options.AccessKey, _OSSService.Options.SecretKey, _OSSService.Options.Endpoint);
GetObjectRequest request = new GetObjectRequest()
{
BucketName = _bucketName,
ObjectKey = objectName,
};
using (GetObjectResponse response = client.GetObject(request))
{
if (FS.TryGetContentType(objectName, out var contentType) || GetContentType(objectName, out contentType))
{
//var localPath = filePrefix + _bucketName + '/' + objectName;
//if (!File.Exists(localPath))
//{
// response.WriteResponseStreamToFile(localPath);
//}
//return new FileStreamResult(new FileStream(localPath, FileMode.Open), contentType) { FileDownloadName = objectName };
var ms = new MemoryStream();
await response.OutputStream.CopyToAsync(ms);
ms.Position = 0;
response.OutputStream.Close();
response.OutputStream.Dispose();
return new FileStreamResult(ms, contentType) { FileDownloadName = objectName };
}
else
{
return await Display(false, "文件格式有问题");
}
}
}
catch (ObsException ex)
{
throw;
}
#endregion
}
private bool GetContentType(string filename, out string contenttype)
{
contenttype = "";
bool rslt = true;
if (filename.ToLower().EndsWith(".dwg"))
{
contenttype = "application/octet-stream";
}
else
rslt = false;
return rslt;
}
/// <summary>
/// 上传文件的数据流。 大小写敏感!
/// </summary>
/// <returns></returns>
[HttpPost]
[DisableRequestSizeLimit]
public async Task<ObsApiOutput> PutObjectAsync([FromBody] PutObjectInput args)
{
if (args == null || string.IsNullOrWhiteSpace(args.path))
return new ObsApiOutput(false, "参数为空");
var file = new FileInfo(args.path);
if (!file.Exists)
return new ObsApiOutput(false, "文件不存在");
var objectName = args.objectName;
var path = args.path;
if (string.IsNullOrWhiteSpace(args.objectName))
objectName = file.Name;
try
{
#region dwg压缩处理
var zipRslt = false;
if (path.ToLower().EndsWith(".dwg"))
{
if (new Tools.ZipHelper().ZipOneFile(path, 0))
{
zipRslt = true;
path += ".zip";
objectName += ".zip";
}
}
#endregion
var r = await _OSSService.PutObjectAsync(_bucketName, objectName, path);
if (r && zipRslt)
{
new FileInfo(path).Delete();
}
return new ObsApiOutput(r, r ? "上传成功" : "上传失败");
}
catch (ObsException ex)
{
return new ObsApiOutput(false, "错误状态码:" + ex.StatusCode);
}
catch (Exception ex)
{
return new ObsApiOutput(false, "异常信息:" + ex.Message + ex.StackTrace);
}
}
private async Task<IActionResult> Display(bool isSuccess, object data)
{
return DisplayJson(new RestfulResult<object>
{
Code = isSuccess ? 200 : 500, // 处理没有返回值情况 204
Success = true,
Data = data,
Message = "请求成功",
Extras = null,
Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
});
}
private IActionResult DisplayJson(object data)
{
return new ContentResult
{
Content = JsonConvert.SerializeObject(data, Formatting.Indented, new JsonSerializerSettings
{
ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver(),
DateFormatString = "yyyy-MM-dd HH:mm:ss"
}),
ContentType = "application/json"
};
}
//public async Task<dynamic> Test()
//{
// // 创建ObsClient实例
// ObsClient client = new ObsClient("C4D30C2801D928AAF687", "ooZVXaB1tqIz7DHTv53RILD7o5cAAAGAAdkoqlR2", "http://10.74.25.87:6020");
// // 列举桶
// try
// {
// ListBucketsRequest request = new ListBucketsRequest();
// ListBucketsResponse response = client.ListBuckets(request);
// return response;
// }
// catch (ObsException ex)
// {
// throw;
// }
//}
}
}

View File

@@ -0,0 +1,49 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Vote.Services.Dto
{
public class ObsInput
{
}
/// <summary>
///
/// </summary>
public class GetObjectInput
{
/// <summary>
/// 大小写敏感!
/// </summary>
public string objectName { get; set; }
}
/// <summary>
///
/// </summary>
public class PutObjectInput
{
/// <summary>
/// 本地文件全路径
/// </summary>
public string path { get; set; }
/// <summary>
/// 希望上传到的目录和文件名 , 如果为空则上传到根目录 ,大小写敏感! ,斜杠必须使用/ , eg: files/COC/202011/11/2020KDFJ0075.pdf
/// </summary>
public string objectName { get; set; }
}
public class ObsApiOutput
{
public ObsApiOutput(bool _bizIsSuccess, string _message, object _data = null)
{
this.bizIsSuccess = _bizIsSuccess;
this.message = _message;
this.data = _data;
}
public bool bizIsSuccess { get; set; }
public string message { get; set; }
public object data { get; set; }
}
}

View File

@@ -0,0 +1,145 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Zip;
namespace Vote.Services.Tools
{
/// <summary>
///
/// </summary>
public class ZipHelper
{
/// <summary>
/// 所有文件缓存
/// </summary>
List<string> files = new List<string>();
/// <summary>
/// 所有空目录缓存
/// </summary>
List<string> paths = new List<string>();
/// <summary>
/// 取得目录下所有文件及文件夹分别存入files及paths
/// </summary>
/// <param name="rootPath">根目录</param>
private void GetAllDirectories(string rootPath)
{
string[] subPaths = Directory.GetDirectories(rootPath);//得到所有子目录
foreach (string path in subPaths)
{
GetAllDirectories(path);//对每一个字目录做与根目录相同的操作即找到子目录并将当前目录的文件名存入List
}
string[] files = Directory.GetFiles(rootPath);
foreach (string file in files)
{
this.files.Add(file);//将当前目录中的所有文件全名存入文件List
}
if (subPaths.Length == files.Length && files.Length == 0)//如果是空目录
{
this.paths.Add(rootPath);//记录空目录
}
}
/// <summary>
/// 压缩目录(包括子目录及所有文件)
/// </summary>
/// <param name="rootPath">要压缩的根目录</param>
/// <param name="destinationPath">保存路径</param>
/// <param name="compressLevel">压缩程度范围0-9数值越大压缩程序越高</param>
public void ZipFileFromDirectory(string rootPath, string destinationPath, int compressLevel)
{
this.files.Clear();
GetAllDirectories(rootPath);
string rootMark = rootPath + "\\";//得到当前路径的位置,以备压缩时将所压缩内容转变成相对路径。
Crc32 crc = new Crc32();
using ZipOutputStream outPutStream = new ZipOutputStream(System.IO.File.Create(destinationPath));
outPutStream.SetLevel(compressLevel); // 0 - store only to 9 - means best compression
foreach (string file in files)
{
using FileStream fileStream = System.IO.File.OpenRead(file);//打开压缩文件
byte[] buffer = new byte[fileStream.Length];
fileStream.Read(buffer, 0, buffer.Length);
ZipEntry entry = new ZipEntry(file.Replace(rootMark, string.Empty));
entry.DateTime = DateTime.Now;
entry.Size = fileStream.Length;
fileStream.Close();
crc.Reset();
crc.Update(buffer);
entry.Crc = crc.Value;
outPutStream.PutNextEntry(entry);
outPutStream.Write(buffer, 0, buffer.Length);
}
this.files.Clear();
foreach (string emptyPath in paths)
{
ZipEntry entry = new ZipEntry(emptyPath.Replace(rootMark, string.Empty) + "/");
outPutStream.PutNextEntry(entry);
}
this.paths.Clear();
outPutStream.Finish();
outPutStream.Close();
GC.Collect();
}
/// <summary>
/// 压缩单个文件
/// <param name="filepath">要压缩的文件路径</param>
/// <param name="compressLevel">压缩程度范围0-9数值越大压缩程序越高</param>
/// <paramref name="dwgZipPath">输出zip文件路径</paramref>
/// </summary>
public bool ZipOneFile(string filepath, int compressLevel)
{
var r = false;
try
{
var file = new FileInfo(filepath);
var dwgZipPath = GetZipFileName(filepath);
Crc32 crc = new Crc32();
using ZipOutputStream outPutStream = new ZipOutputStream(System.IO.File.Create(dwgZipPath));
outPutStream.SetLevel(compressLevel); // 0 - store only to 9 - means best compression
using FileStream fileStream = System.IO.File.OpenRead(filepath);//打开压缩文件
byte[] buffer = new byte[fileStream.Length];
fileStream.Read(buffer, 0, buffer.Length);
ZipEntry entry = new ZipEntry(filepath.Replace(file.DirectoryName + "\\", string.Empty));
entry.DateTime = DateTime.Now;
entry.Size = fileStream.Length;
fileStream.Close();
crc.Reset();
crc.Update(buffer);
entry.Crc = crc.Value;
outPutStream.PutNextEntry(entry);
outPutStream.Write(buffer, 0, buffer.Length);
this.files.Clear();
this.paths.Clear();
outPutStream.Finish();
outPutStream.Close();
GC.Collect();
r = true;
}
catch (Exception ex)
{
r = false;
}
return r;
}
private string GetZipFileName(string filepath)
{
//if (System.IO.File.Exists(filepath + ".zip"))
//{
// return GetZipFileName(filepath + ".zip");
//}
return filepath + ".zip";
}
}
}

View File

@@ -12,6 +12,7 @@
<PackageReference Include="Aspose.Words" Version="23.1.0" />
<PackageReference Include="Furion" Version="4.5.0" />
<PackageReference Include="HtmlAgilityPack" Version="1.11.46" />
<PackageReference Include="OnceMi.AspNetCore.OSS" Version="1.1.9" />
</ItemGroup>
<ItemGroup>

View File

@@ -4,6 +4,47 @@
<name>Vote.Services</name>
</assembly>
<members>
<member name="T:Vote.Services.ApiController.HuaweiService">
<summary>
OBS
</summary>
</member>
<member name="M:Vote.Services.ApiController.HuaweiService.#ctor(OnceMi.AspNetCore.OSS.IOSSService)">
<summary>
</summary>
<param name="OSSService"></param>
</member>
<member name="M:Vote.Services.ApiController.HuaweiService.ListBuckets">
<summary>
列出当前账号下允许访问的所有储存桶。
</summary>
<returns></returns>
</member>
<member name="M:Vote.Services.ApiController.HuaweiService.GetBucketAclAsync">
<summary>
获取储存桶的外部访问权限。
</summary>
<returns></returns>
</member>
<member name="M:Vote.Services.ApiController.HuaweiService.ObjectsExistsAsync(Vote.Services.Dto.GetObjectInput)">
<summary>
获取指定储存桶中指定对象是否存在。
</summary>
<returns></returns>
</member>
<member name="M:Vote.Services.ApiController.HuaweiService.DownloadObjectAsync(Vote.Services.Dto.GetObjectInput)">
<summary>
获取文件的数据流。 大小写敏感!
</summary>
<returns></returns>
</member>
<member name="M:Vote.Services.ApiController.HuaweiService.PutObjectAsync(Vote.Services.Dto.PutObjectInput)">
<summary>
上传文件的数据流。 大小写敏感!
</summary>
<returns></returns>
</member>
<member name="T:Vote.Services.ApiController.ProjectsService">
<summary>
项目
@@ -56,6 +97,31 @@
</summary>
<returns></returns>
</member>
<member name="T:Vote.Services.Dto.GetObjectInput">
<summary>
</summary>
</member>
<member name="P:Vote.Services.Dto.GetObjectInput.objectName">
<summary>
大小写敏感!
</summary>
</member>
<member name="T:Vote.Services.Dto.PutObjectInput">
<summary>
</summary>
</member>
<member name="P:Vote.Services.Dto.PutObjectInput.path">
<summary>
本地文件全路径
</summary>
</member>
<member name="P:Vote.Services.Dto.PutObjectInput.objectName">
<summary>
希望上传到的目录和文件名 , 如果为空则上传到根目录 ,大小写敏感! ,斜杠必须使用/ , eg: files/COC/202011/11/2020KDFJ0075.pdf
</summary>
</member>
<member name="P:Vote.Services.Dto.ProjectsInput.type">
<summary>
项目类型
@@ -343,5 +409,42 @@
<param name="strHtml"></param>
<returns></returns>
</member>
<member name="T:Vote.Services.Tools.ZipHelper">
<summary>
</summary>
</member>
<member name="F:Vote.Services.Tools.ZipHelper.files">
<summary>
所有文件缓存
</summary>
</member>
<member name="F:Vote.Services.Tools.ZipHelper.paths">
<summary>
所有空目录缓存
</summary>
</member>
<member name="M:Vote.Services.Tools.ZipHelper.GetAllDirectories(System.String)">
<summary>
取得目录下所有文件及文件夹分别存入files及paths
</summary>
<param name="rootPath">根目录</param>
</member>
<member name="M:Vote.Services.Tools.ZipHelper.ZipFileFromDirectory(System.String,System.String,System.Int32)">
<summary>
压缩目录(包括子目录及所有文件)
</summary>
<param name="rootPath">要压缩的根目录</param>
<param name="destinationPath">保存路径</param>
<param name="compressLevel">压缩程度范围0-9数值越大压缩程序越高</param>
</member>
<member name="M:Vote.Services.Tools.ZipHelper.ZipOneFile(System.String,System.Int32)">
<summary>
压缩单个文件
<param name="filepath">要压缩的文件路径</param>
<param name="compressLevel">压缩程度范围0-9数值越大压缩程序越高</param>
<paramref name="dwgZipPath">输出zip文件路径</paramref>
</summary>
</member>
</members>
</doc>