注意:記得引進Oss 相關的SDK,一般直接從Nuget包引入就行
private static string endpoint = "";//OSS通路域名
private static string accessKeyId = "";//OSS密鑰
private static string accessKeySecret = "";//OSS密鑰
private static string returnUrl = "";//上傳傳回位址
private static string bucketName = "";//存儲空間
//imgstream:上傳檔案或視訊檔案流
//name:檔案辨別名
//path:存儲空間名
public static string UpToAliYunImgage(Stream imgstream, string name, string path = "")
{
if (imgstream == null)
{
return "";
}
try
{
var key = name;
if (!string.IsNullOrWhiteSpace(path))
{
key = path + "/" + key;
}
OssClient oss = new OssClient(endpoint, accessKeyId, accessKeySecret);
imgstream.Position = 0;
oss.PutObject(bucketName, key, imgstream);
//ObjectMetadata oa = new ObjectMetadata();
//oa.AddHeader("Content-Disposition", "attachment");//設定oss檔案為強制下載下傳
//oss.PutObject(bucketName, key, imgstream, oa);
imgstream.Dispose();//上傳完記得釋放檔案流
return returnUrl + key;
}
catch (Exception e)
{
return "";
}
}