注意:记得引进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 "";
}
}