| | |
| | | using Aliyun.Acs.Core.Auth.Sts; |
| | | using Aliyun.Acs.Core.Http; |
| | | using Aliyun.Acs.Core.Profile; |
| | | using Aliyun.OSS; |
| | | using Furion; |
| | | using Furion.DistributedIDGenerator; |
| | | using Furion.HttpRemote; |
| | | using Microsoft.AspNetCore.Http; |
| | | using Microsoft.CodeAnalysis; |
| | | using Microsoft.Extensions.Options; |
| | | using System; |
| | | using System.Collections.Generic; |
| | | using System.Linq; |
| | | using System.Text; |
| | | using System.Diagnostics; |
| | | using System.IO; |
| | | using System.Threading.Tasks; |
| | | using static System.Runtime.InteropServices.JavaScript.JSType; |
| | | |
| | | namespace FlexJobApi.Core |
| | | { |
| | |
| | | }; |
| | | return result; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 上传文件 |
| | | /// </summary> |
| | | /// <param name="scene"></param> |
| | | /// <param name="url"></param> |
| | | /// <param name="filename"></param> |
| | | /// <returns></returns> |
| | | public static AliyunOSSUploadResult Upload(string scene, string url, string filename = null) |
| | | { |
| | | if (filename.IsNull()) |
| | | { |
| | | filename = url.Substring(url.LastIndexOf('/') + 1); |
| | | } |
| | | var stream = App.GetRequiredService<IHttpRemoteService>().GetAsStream(url); |
| | | return Upload(scene, stream, filename); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 上传文件 |
| | | /// </summary> |
| | | /// <param name="url"></param> |
| | | /// <param name="key"></param> |
| | | /// <returns></returns> |
| | | public static AliyunOSSUploadResult Upload(string url, string key) |
| | | { |
| | | var stream = App.GetRequiredService<IHttpRemoteService>().GetAsStream(url); |
| | | return Upload(stream, key); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 上传文件 |
| | | /// </summary> |
| | | /// <param name="scene"></param> |
| | | /// <param name="file"></param> |
| | | /// <param name="filename"></param> |
| | | /// <returns></returns> |
| | | public static AliyunOSSUploadResult Upload(string scene, IFormFile file, string filename = null) |
| | | { |
| | | var stream = file.OpenReadStream(); |
| | | filename = filename ?? file.FileName; |
| | | return Upload(scene, stream, filename); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 上传文件 |
| | | /// </summary> |
| | | /// <param name="scene"></param> |
| | | /// <param name="stream"></param> |
| | | /// <param name="filename"></param> |
| | | /// <returns></returns> |
| | | public static AliyunOSSUploadResult Upload(string scene, Stream stream, string filename) |
| | | { |
| | | var key = $"Resource/FlexJob/{scene}/{DateTime.Now:yyyy-MM-dd}/{DateTime.Now.ToTimeStamp()}-{IDGen.NextID()}/{filename}"; |
| | | return Upload(stream, key); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 上传文件 |
| | | /// </summary> |
| | | /// <param name="stream"></param> |
| | | /// <param name="key"></param> |
| | | /// <returns></returns> |
| | | public static AliyunOSSUploadResult Upload(Stream stream, string key) |
| | | { |
| | | var options = App.GetOptions<AliyunOptions>().OSS; |
| | | var client = new OssClient(options.Endpoint, options.AccessKeyId, options.AccessSecret); |
| | | client.PutObject(options.BucketName, key, stream); |
| | | var result = new AliyunOSSUploadResult |
| | | { |
| | | Url = key |
| | | }; |
| | | stream.Seek(0, SeekOrigin.Begin); |
| | | result.Buffer = new byte[stream.Length]; |
| | | stream.ReadExactly(result.Buffer); |
| | | return result; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 获取Url |
| | | /// </summary> |
| | | /// <param name="url"></param> |
| | | /// <param name="process"></param> |
| | | /// <returns></returns> |
| | | public static string GetUrl(string url, string process = null) |
| | | { |
| | | var options = App.GetOptions<AliyunOptions>().OSS; |
| | | var client = new OssClient(options.Endpoint, options.AccessKeyId, options.AccessSecret); |
| | | var req = new GeneratePresignedUriRequest(options.BucketName, url.TrimStart('/'), SignHttpMethod.Get) |
| | | { |
| | | Expiration = DateTime.Now.AddHours(1), |
| | | Process = process |
| | | }; |
| | | url = client.GeneratePresignedUri(req).ToString(); |
| | | return url; |
| | | } |
| | | } |
| | | } |