| | |
| | | using Aliyun.Acs.Core.Profile; |
| | | using Aliyun.OSS; |
| | | using Furion; |
| | | using Furion.DistributedIDGenerator; |
| | | using Furion.HttpRemote; |
| | | using Microsoft.AspNetCore.Http; |
| | | using Microsoft.CodeAnalysis; |
| | |
| | | /// <param name="url"></param> |
| | | /// <param name="filename"></param> |
| | | /// <returns></returns> |
| | | public static string Upload(string scene, string url, string filename = null) |
| | | 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> |
| | |
| | | /// <param name="file"></param> |
| | | /// <param name="filename"></param> |
| | | /// <returns></returns> |
| | | public static string Upload(string scene, IFormFile file, string filename = null) |
| | | public static AliyunOSSUploadResult Upload(string scene, IFormFile file, string filename = null) |
| | | { |
| | | var stream = file.OpenReadStream(); |
| | | filename = filename ?? file.FileName; |
| | | return Upload(scene, stream, filename); |
| | | } |
| | | |
| | |
| | | /// <param name="stream"></param> |
| | | /// <param name="filename"></param> |
| | | /// <returns></returns> |
| | | public static string Upload(string scene, Stream stream, string filename = null) |
| | | 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); |
| | | var key = $"Resource/{scene}/{DateTime.Now:yyyy-MM-dd}/{filename}"; |
| | | client.PutObject(options.BucketName, key, stream); |
| | | return key; |
| | | var result = new AliyunOSSUploadResult |
| | | { |
| | | Url = key |
| | | }; |
| | | stream.Seek(0, SeekOrigin.Begin); |
| | | result.Buffer = new byte[stream.Length]; |
| | | stream.ReadExactly(result.Buffer); |
| | | return result; |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | { |
| | | var options = App.GetOptions<AliyunOptions>().OSS; |
| | | var client = new OssClient(options.Endpoint, options.AccessKeyId, options.AccessSecret); |
| | | var req = new GeneratePresignedUriRequest(options.BucketName, url, SignHttpMethod.Get) |
| | | var req = new GeneratePresignedUriRequest(options.BucketName, url.TrimStart('/'), SignHttpMethod.Get) |
| | | { |
| | | Expiration = DateTime.Now.AddHours(1), |
| | | Process = process |