/*
* Copyright (C) Alibaba Cloud Computing
* All rights reserved.
*
* 版权所有 (C)阿里云计算有限公司
*/
using System;
using System.Collections.Generic;
using Aliyun.OSS;
using Aliyun.OSS.Util;
namespace Aliyun.OSS.Common
{
///
/// 表示访问阿里云服务的配置信息。
///
public class ClientConfiguration : ICloneable
{
private const string UserAgentPrefix = "aliyun-sdk-dotnet/";
private static readonly string _userAgent = GetDefaultUserAgent();
// A temporary solution for undesired abortion when putting/getting large objects,
// asynchronous implementions of putobject/getobject in next version can be a better
// solution to solve such problems.
private int _connectionTimeout = -1;
private int _maxErrorRetry = 3;
private int _proxyPort = -1;
private bool _isCname = false;
///
/// HttpWebRequest最大的并发连接数目。
///
public const int ConnectionLimit = 512;
private Protocol _protocol = Protocol.Http;
///
/// 获取访问请求的User-Agent。
///
public string UserAgent
{
get { return _userAgent; }
}
///
/// 获取或设置代理服务器的地址。
///
public string ProxyHost { get; set; }
///
/// 获取或设置代理服务器的端口。
///
public int ProxyPort
{
get { return _proxyPort; }
set { _proxyPort = value; }
}
///
/// 获取或设置用户名。
///
public string ProxyUserName { get; set; }
///
/// 获取或设置密码。
///
public string ProxyPassword { get; set; }
///
/// 获取或设置代理服务器授权用户所在的域。
///
public string ProxyDomain { get; set; }
///
/// 获取或设置连接超时时间,单位为毫秒。
///
public int ConnectionTimeout
{
get { return _connectionTimeout; }
set { _connectionTimeout = value; }
}
///
/// 获取或设置请求发生错误时最大的重试次数。
///
public int MaxErrorRetry
{
get { return _maxErrorRetry; }
set { _maxErrorRetry = value; }
}
///
/// 获取或设置请求OSS服务时采用的通信协议。
///
public Protocol Protocol
{
get { return _protocol; }
set { _protocol = value; }
}
///
/// 获取或设置请求的Endpoint是否是cname
/// 如果是cname,不支持ListBuckets操作
///
public bool IsCname
{
get
{
return _isCname;
}
set
{
_isCname = value;
}
}
///
/// 设置自定义基准时间。
///
/// 由于OSS的token校验是时间相关的,可能会因为终端系统时间不准导致无法访问OSS服务。
/// 通过该接口设置自定义Epoch秒数,SDK计算出本机当前时间与自定义时间的差值,之后的
/// 每次请求时间均加上该差值,以达到时间校验的目的。
///
///
/// 自定义Epoch秒数。
public void SetCustomEpochTicks(long epochTicks)
{
var epochTime = new DateTime(1970, 1, 1);
var timeSpan = DateTime.UtcNow.Subtract(epochTime);
var localTicks = (long)timeSpan.TotalSeconds;
TickOffset = epochTicks - localTicks;
}
///
/// 获取自定义基准时间与本地时间的时差值,单位为秒。
///
public long TickOffset { get; internal set; }
///
/// 获取该实例的拷贝。
///
/// 该实例的拷贝。
public object Clone()
{
return new ClientConfiguration
{
ConnectionTimeout = ConnectionTimeout,
MaxErrorRetry = MaxErrorRetry,
ProxyDomain = ProxyDomain,
ProxyHost = ProxyHost,
ProxyPassword = ProxyPassword,
ProxyPort = ProxyPort,
ProxyUserName = ProxyUserName,
TickOffset = TickOffset
};
}
///
/// 获取User-Agent信息。
///
private static string GetDefaultUserAgent()
{
return UserAgentPrefix +
typeof(ClientConfiguration).Assembly.GetName().Version + "(" +
OssUtils.DetermineOsVersion() + "/" +
Environment.OSVersion.Version + "/" +
OssUtils.DetermineSystemArchitecture() + ";" +
Environment.Version + ")";
}
}
}