sunpengfei
3 天以前 c33b59a63a99a4598d6d6bc56750efd4f7ad550d
FlexJobApi.Core/Utils/AliyunUtils/AliyunOSSUtils.cs
@@ -4,6 +4,7 @@
using Aliyun.Acs.Core.Profile;
using Aliyun.OSS;
using Furion;
using Furion.DistributedIDGenerator;
using Furion.HttpRemote;
using Microsoft.AspNetCore.Http;
using Microsoft.CodeAnalysis;
@@ -51,10 +52,26 @@
        /// <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>
@@ -64,9 +81,10 @@
        /// <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);
        }
@@ -77,13 +95,31 @@
        /// <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>
@@ -96,7 +132,7 @@
        {
            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