| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- using Aliyun.OSS;
- using DaJiaoYan.Models;
- using DaJiaoYan.Utils;
- using System;
- using System.IO;
- using System.Text.RegularExpressions;
- namespace DaJiaoYan.Services
- {
- public static class AliyunOss
- {
- private static AliyunOssClient client = null;
- /// <summary>
- /// 客户端锁
- /// </summary>
- private readonly static object ClientLock = new object();
- private static AliyunOssClient Client
- {
- get
- {
- if (client == null)
- {
- lock (ClientLock)
- {
- client = new AliyunOssClient();
- }
- }
- return client;
- }
- }
- /// <summary>
- /// 上传
- /// </summary>
- /// <param name="bucketName"></param>
- /// <param name="objectName"></param>
- /// <param name="requestContent"></param>
- /// <returns></returns>
- public static bool SimpleUpload(string objectName, MemoryStream requestContent)
- {
- return Client.SimpleUpload(objectName, requestContent);
- }
- /// <summary>
- /// 上传
- /// </summary>
- /// <param name="bucketName"></param>
- /// <param name="objectName"></param>
- /// <param name="localFilename"></param>
- /// <returns></returns>
- public static bool SimpleUpload(string objectName, string localFilename)
- {
- return Client.SimpleUpload(objectName, localFilename);
- }
- }
- public class AliyunOssClient
- {
- public delegate void DeleWriteLog(string txt);
- public static DeleWriteLog WriteLog;
- private static OssClient client = null;
- private static AliyunAccessModel accessModel = null;
- /// <summary>
- /// 客户端锁
- /// </summary>
- private readonly static object ClientLock = new object();
- /// <summary>
- /// 授权有效期临界值
- /// </summary>
- private const int EXP_THRESHOLD = 60 * 2 * 1000;
- private readonly string token = null;
- public AliyunOssClient(string token = null)
- {
- this.token = token;
- }
- private AliyunAccessModel Access
- {
- get
- {
- lock (ClientLock)
- {
- if (accessModel == null || accessModel.Expiration - EXP_THRESHOLD < Functions.GetTimeStamp())
- {
- //lock (accessModel)
- //{
- accessModel = Api.GetAliyunAccess(token).Result;
- if (accessModel != null)
- {
- accessModel.Expiration = Functions.GetTimeStamp() + accessModel.Expiration * 1000;
- accessModel.EndPoint = Regex.Replace(accessModel.EndPoint, @"-internal", "", RegexOptions.IgnoreCase);
- }
- //}
- }
- }
- return accessModel;
- }
- }
- public bool AccessAvailable
- {
- get
- {
- return !string.IsNullOrEmpty(Access?.EndPoint) && !string.IsNullOrEmpty(Access?.AccessKeyId) && !string.IsNullOrEmpty(Access?.AccessKeySecret);
- }
- }
- private OssClient Client
- {
- get
- {
- if (client == null && AccessAvailable)
- {
- client = new OssClient(Access?.EndPoint, Access?.AccessKeyId, Access?.AccessKeySecret);
- }
- return client;
- }
- }
- /// <summary>
- /// 上传
- /// </summary>
- /// <param name="bucketName"></param>
- /// <param name="objectName"></param>
- /// <param name="requestContent"></param>
- /// <returns></returns>
- public bool SimpleUpload(string objectName, Stream requestContent)
- {
- //// yourEndpoint填写Bucket所在地域对应的Endpoint。以华东1(杭州)为例,Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。
- //var endpoint = "yourEndpoint";
- //// 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。
- //var accessKeyId = "yourAccessKeyId";
- //var accessKeySecret = "yourAccessKeySecret";
- //// 填写Bucket名称。
- //var bucketName = "examplebucket";
- //// 填写Object完整路径。Object完整路径中不能包含Bucket名称。
- //var objectName = "exampleobject.txt";
- //// 填写字符串。
- //var objectContent = "More than just cloud.";
- // 创建OssClient实例。
- //var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
- bool res = false;
- try
- {
- //byte[] binaryData = Encoding.ASCII.GetBytes(objectContent);
- //MemoryStream requestContent = new MemoryStream(binaryData);
- // 上传文件。
- Client?.PutObject(Access?.BucketName, objectName, requestContent);
- res = true;
- }
- catch (Exception ex)
- {
- WriteLog?.Invoke($"Put object failed, {ex.Message}");
- WriteLog?.Invoke(ex.StackTrace);
- }
- return res;
- }
- /// <summary>
- /// 上传
- /// </summary>
- /// <param name="bucketName"></param>
- /// <param name="objectName"></param>
- /// <param name="localFilename"></param>
- /// <returns></returns>
- public bool SimpleUpload(string objectName, string localFilename)
- {
- bool res = false;
- //PutObjectResult r;
- try
- {
- //byte[] binaryData = Encoding.ASCII.GetBytes(objectContent);
- //MemoryStream requestContent = new MemoryStream(binaryData);
- for (int i = 0; i < 5; i++)
- {
- using (PutObjectResult r = Client?.PutObject(Access?.BucketName, objectName, localFilename))
- {
- //r = t.
- if (r?.HttpStatusCode == System.Net.HttpStatusCode.OK)
- {
- res = true;
- break;
- }
- }
- }
- // 上传文件。
- }
- catch (Exception ex)
- {
- WriteLog?.Invoke($"Put object failed, {ex.Message}");
- WriteLog?.Invoke(ex.StackTrace);
- }
- return res;
- }
- }
- }
|