/*
OSS
帮助类
*/
using Aliyun.OSS; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace OSSDemo { public class OSSClientHelper { const string accessKeyId = ""; const string accessKeySecret = ""; const string endpoint = "http://oss-cn-beijing.aliyuncs.com"; const string bucketName = "";// public static OssClient GetClient() { return new OssClient(endpoint, accessKeyId, accessKeySecret); } /// <summary> /// 上传一个图片 /// </summary> /// <param name="base64Code">图片经过base64加密后的结果</param> /// <param name="fileName">文件名,例如:Emplyoee/dzzBack.jpg</param> public static bool PushImg(string base64Code, string fileName) { try { var client = GetClient(); MemoryStream stream = new MemoryStream(Convert.FromBase64String(base64Code)); return client.PutObject(bucketName, fileName, stream).HttpStatusCode== System.Net.HttpStatusCode.OK; } catch (Exception) { } return false; } /// <summary> /// 上传一个图片 /// </summary> /// <param name="filebyte">图片字节 </param> /// <param name="fileName">文件名,例如:Emplyoee/dzzBack.jpg</param> public static bool PushImg(byte[] filebyte, string fileName) { try { var client = GetClient(); MemoryStream stream = new MemoryStream(filebyte,0,filebyte.Length); return client.PutObject(bucketName, fileName, stream).HttpStatusCode == System.Net.HttpStatusCode.OK; } catch (Exception) { } return false; } /// <summary> /// 获取鉴权后的URL,URL有效日期默认一小时 /// </summary> /// <param name="fileName">文件名,例如:Emplyoee/dzzBack.jpg</param> /// <returns></returns> public static string GetImg(string fileName) { var client = GetClient(); var key = fileName; var req = new GeneratePresignedUriRequest(bucketName, key, SignHttpMethod.Get) { Expiration = DateTime.Now.AddHours(1) }; return client.GeneratePresignedUri(req).ToString(); } /// <summary> /// 获取鉴权后的URL /// </summary> /// <param name="fileName">文件名,例如:Emplyoee/dzzBack.jpg</param> /// <param name="expiration">URL有效日期,例如:DateTime.Now.AddHours(1) </param> /// <returns></returns> public static string GetImg(string fileName, DateTime expiration) { var client = GetClient(); var key = fileName; var req = new GeneratePresignedUriRequest(bucketName, key, SignHttpMethod.Get) { Expiration = expiration }; return client.GeneratePresignedUri(req).ToString(); } } }
/*调用*/ using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Aliyun.OSS; using System.IO; using System.Diagnostics; namespace OSSDemo { class Program { static void Main(string[] args) { string fileName = "IMEI/111.jpg"; //上传一张图片 var pushRes = OSSClientHelper.PushImg(File.ReadAllBytes(@"D:1.jpg"), fileName); //获取临时URL 超时时间30秒 var url = OSSClientHelper.GetImg(fileName, DateTime.Now.AddSeconds(30)); Console.ReadKey(); } } }
// 2018-11-20 更新帮助类。
using Aliyun.OSS; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace zz.Oss { /// <summary> /// 阿里OSS帮助类 /// </summary> public class OssALiHelper { private string _accessKeyId = string.Empty; private string _accessKeySecret = string.Empty; private string _endpoint = string.Empty; private string _bucketName = string.Empty; /// <summary> /// 实例化构造函数 /// </summary> /// <param name="accessKeyId">accessKeyId,例如:LaTAI9o9qf9iC1Qxfyf4t</param> /// <param name="accessKeySecret">Secret,例如:xITragGClDJsfsQfIbHSCfebSJdcjsfBv4jSP</param> /// <param name="endpoint">地域节点,例如:http://oss-cn-beijing.aliyuncs.com</param> /// <param name="bucketName">bucket名称,该名称在阿里云是唯一的,申请时请注意。例如:drumbeat</param> public OssALiHelper(string accessKeyId, string accessKeySecret, string endpoint, string bucketName) { _accessKeyId = accessKeyId ?? throw new ArgumentNullException(nameof(accessKeyId)); _accessKeySecret = accessKeySecret ?? throw new ArgumentNullException(nameof(accessKeySecret)); _endpoint = endpoint ?? throw new ArgumentNullException(nameof(endpoint)); _bucketName = bucketName ?? throw new ArgumentNullException(nameof(bucketName)); } public OssClient GetClient() { return new OssClient(_endpoint, _accessKeyId, _accessKeySecret); } /// <summary> /// 上传一个图片 /// </summary> /// <param name="base64Code">图片经过base64加密后的结果</param> /// <param name="fileName">文件名,例如:Emplyoee/dzzBack.jpg</param> public bool PushImg(string base64Code, string fileName) { try { var client = GetClient(); MemoryStream stream = new MemoryStream(Convert.FromBase64String(base64Code)); return client.PutObject(_bucketName, fileName, stream).HttpStatusCode == System.Net.HttpStatusCode.OK; } catch (Exception) { } return false; } /// <summary> /// 上传一个文件 /// </summary> /// <param name="filebyte">图片字节 </param> /// <param name="fileName">文件名,例如:Emplyoee/dzzBack.jpg</param> public bool PushFile(byte[] filebyte, string fileName) { try { var client = GetClient(); MemoryStream stream = new MemoryStream(filebyte, 0, filebyte.Length); return client.PutObject(_bucketName, fileName, stream).HttpStatusCode == System.Net.HttpStatusCode.OK; } catch (Exception) { } return false; } /// <summary> /// 获取鉴权后的URL,文件过期时间默认设置为100年 /// </summary> /// <param name="fileName">文件名,例如:Emplyoee/dzzBack.jpg</param> /// <returns></returns> public string GetFileUrl(string fileName) { var client = GetClient(); var key = fileName; var req = new GeneratePresignedUriRequest(_bucketName, key, SignHttpMethod.Get) { Expiration = DateTime.Now.AddYears(100) }; return client.GeneratePresignedUri(req).ToString(); } /// <summary> /// 获取鉴权后的URL /// </summary> /// <param name="fileName">文件名,例如:Emplyoee/dzzBack.jpg</param> /// <param name="expiration">URL有效日期,例如:DateTime.Now.AddHours(1) </param> /// <returns></returns> public string GetFileUrl(string fileName, DateTime expiration) { var client = GetClient(); var key = fileName; var req = new GeneratePresignedUriRequest(_bucketName, key, SignHttpMethod.Get) { Expiration = expiration }; return client.GeneratePresignedUri(req).ToString(); } } }