using System;
using System.IO;
using Aliyun.OSS;
using Aliyun.OSS.Common;
using Aliyun.OSS.Util;
namespace Samples
{
public class Program
{
public static void Main(string[] args)
{
var endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
var bucketName = "examplebucket";
var objectName = "exampledir/example.jpg";
var localImageFilename = "D:\\localpath\\example.jpg";
const string region = "cn-hangzhou";
var conf = new ClientConfiguration();
conf.SignatureVersion = SignatureVersion.V4;
var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);
client.SetRegion(region);
try
{
var process = "image/resize,m_fixed,w_100,h_100";
var ossObject = client.GetObject(new GetObjectRequest(bucketName, objectName, process));
WriteToFile(localImageFilename, ossObject.Content);
process = "image/crop,w_100,h_100,x_100,y_100";
ossObject = client.GetObject(new GetObjectRequest(bucketName, objectName, process));
WriteToFile(localImageFilename , ossObject.Content);
process = "image/rotate,90";
ossObject = client.GetObject(new GetObjectRequest(bucketName, objectName, process));
WriteToFile(localImageFilename , ossObject.Content);
}
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);
}
}
private static void WriteToFile(string filePath, Stream stream)
{
using (var requestStream = stream)
{
using (var fs = File.Open(filePath, FileMode.OpenOrCreate))
{
IoUtils.WriteTo(stream, fs);
}
}
}
}
}