/*
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();
}
}
}
