NewGaoKaoApi/New_College.Common/Helper/AliYunOssHelper.cs

45 lines
1.3 KiB
C#

using Aliyun.OSS;
using Aliyun.OSS.Common;
using System;
using System.Collections.Generic;
using System.Text;
namespace New_College.Common
{
public class AliYunOssHelper
{
// private readonly static AliYunOssConfig ossConfig = GlobalData.AliYunOss;
public static string UploadFile(string key, string filepath)
{
try
{
var conf = new ClientConfiguration();
//conf.ConnectionLimit = 512;
conf.MaxErrorRetry = 3;
conf.ConnectionTimeout = 10000*30;
conf.EnalbeMD5Check = true;
var client = new OssClient(AliYunOssConfig.endpoint, AliYunOssConfig.accessKeyId, AliYunOssConfig.accessKeySecret, conf);
try
{
// 上传文件。
var cc = client.PutObject(AliYunOssConfig.bucket, key, filepath);
var newurl = string.Format("{0}/{1}", AliYunOssConfig.wendpoint, key);
return newurl;
}
catch (ClientException ex)
{
Console.WriteLine("Put object failed, {0}", ex.Message);
}
}
catch (Exception ex)
{
throw ex;
}
return "";
}
}
}