public static classAliyunOSS {static string accessKeyId =Config.AccessKeyId;static string accessKeySecret =Config.AccessKeySecret;static string endpoint =Config.Endpoint;static OssClient client = newOssClient(endpoint, accessKeyId, accessKeySecret);static string fileToUpload =Config.FileToUpload;static double Expiration =Config.Expiration;public static void PutObjectFromFile(string bucketName, string key, string ContentType = "image/jpeg") {try{using (var content =File.Open(fileToUpload, FileMode.Open)) {var metadata = newObjectMetadata();
metadata.ContentLength=content.Length;
metadata.ContentType=ContentType;
client.PutObject(bucketName, key, content, metadata);
}//Console.WriteLine("Put object:{0} succeeded", key);
}catch(OssException ex) {//Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",//ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
}catch(Exception ex) {//Console.WriteLine("Failed with error info: {0}", ex.Message);
}
}public static void PutObject(string bucketName, stringkey, Stream content, ObjectMetadata metadata) {try{
client.PutObject(bucketName, key, content, metadata);
}catch(OssException ex) {
}catch(Exception ex) {
}
}public static void DeleteObject(string bucketName, stringkey)
{try{
client.DeleteObject(bucketName, key);
}catch(OssException ex)
{
}catch(Exception ex)
{
}
}public static void PutObject(string bucketName, stringkey, Stream content)
{try{
client.PutObject(bucketName, key, content);
}catch(OssException ex)
{
}catch(Exception ex)
{
}
}public static string PutLocalFileBySignedUrl(string bucketName, stringkey) {
OssClient client= newOssClient(endpoint, accessKeyId, accessKeySecret);try{var request = newGeneratePresignedUriRequest(bucketName, key, SignHttpMethod.Get);
request.Expiration=DateTime.Now.AddMinutes(Expiration);returnclient.GeneratePresignedUri(request).AbsoluteUri;
}catch(OssException ex) {//Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",//ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
return null;
}catch(Exception ex) {//Console.WriteLine("Failed with error info: {0}", ex.Message);
return null;
}
}public static bool DoesObjectExist(string bucketName, stringkey)
{
OssClient client= newOssClient(endpoint, accessKeyId, accessKeySecret);int i = 0;
GoConn:try{returnclient.DoesObjectExist(bucketName, key);
}catch(OssException ex)
{if (i < 3)
{++i;gotoGoConn;
}throw;
}
}
}