An OSSClient serves as the Object Storage Service (OSS) client for .NET to manage OSS resources such as buckets and objects.
Create an OSSClient instance
You can use one of the following methods to create an OSSClient instance:
(Recommended) Use the V4 signature algorithm
(Not recommended) Use the V1 signature algorithm
We recommend that you use the V4 signature algorithm which provides better security. If you use the V4 signature algorithm, include the region parameter. The region parameter must be an Alibaba Cloud Region ID. Example: cn-Hangzhou
. You must also declare SignVersion.V4. OSS SDK for .NET 2.14.0 and later support the V4 signature algorithm.
The following sample code provides an example on how to create an OSSClient instance by using an OSS endpoint and the V4 signature algorithm. To create an OSSClient instance by using a custom domain name or access credentials obtained from Security Token Service (STS), modify the following sample code based on your actual scenario.
using Aliyun.OSS;
using Aliyun.OSS.Common;
var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
var accessKeySecret=Environment.GetEnvironmentVariable ("OSS_ACCESS_KEY_SECRET");
const string endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
const string region = "cn-hangzhou";
var conf = new ClientConfiguration();
conf.SignatureVersion = SignatureVersion.V4;
var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);
client.SetRegion(region);
Important
From March 1, 2025, the V1 signature algorithm of OSS is no longer available to new customers with new UIDs. From September 1, 2025, OSS no longer updates and maintains the V1 signature algorithm, and the V1 signature algorithm is no longer available for new buckets. Upgrade V1 signatures to V4 signatures at the earliest opportunity to prevent impact on your business.
Use an OSS endpoint to create an OSSClient instance
Use a custom domain name to create an OSSClient instance
Use access credentials obtained from STS to create an OSSClient instance
The following sample code provides an example on how to create an OSSClient instance by using an OSS endpoint:
using Aliyun.OSS;
var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
const string endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
var ossClient = new OssClient(endpoint, accessKeyId, accessKeySecret);
The following sample code provides an example on how to create an OSSClient instance by using a custom domain name.
Important
ossClient.listBuckets is unavailable if a custom domain name is used.
using Aliyun.OSS;
using Aliyun.OSS.Common;
var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
const string endpoint = "yourDomain";
var conf = new ClientConfiguration();
conf.IsCname = true;
var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);
The following sample code provides an example on how to create an OSSClient instance by using access credentials obtained from STS:
using Aliyun.OSS;
var accessKeyId = Environment.GetEnvironmentVariable("YOUR_ACCESS_KEY_ID");
var accessKeySecret = Environment.GetEnvironmentVariable("YOUR_ACCESS_KEY_ID");
const string securityToken = "yourSecurityToken";
const string endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
var ossClient = new OssClient(endpoint, accessKeyId, accessKeySecret, securityToken);
Configure an OSSClient instance
ClientConfiguration is a configuration class of an OSSClient instance. You can use ClientConfiguration to configure parameters, such as the proxy, the connection timeout period, and the maximum number of connections. The following table describes the parameters.
Parameter | Description | Default value |
Parameter | Description | Default value |
ConnectionLimit | The maximum number of concurrent connections. | 512 |
MaxErrorRetry | The maximum number of retries when a request error occurs. | 3 |
ConnectionTimeout | The timeout period for connections. Unit: milliseconds. | -1 (indicates that the connection never times out) |
EnalbeMD5Check | Specifies whether to enable MD5 verification when you upload or download objects. Important The system performance may be compromised if MD5 verification is enabled. | false |
IsCname | Specifies whether a CNAME can be used as an endpoint to create the OSSClient instance. CNAME is used to bind a custom domain name to a bucket. | false |
ProgressUpdateInterval | The update interval of the progress bar. Unit: bytes. | 8096 |
ProxyHost | The proxy server. Example: example.aliyundoc.com . | None |
ProxyPort | The proxy port. Example: 3128 or 8080 . | None |
ProxyUserName | The username used to access the proxy network. This parameter is optional. | None |
ProxyPassword | The password used to access the proxy network. This parameter is optional. | None |
The following sample code provides an example on how to configure an OSSClient instance:
using Aliyun.OSS;
using Aliyun.OSS.Common;
var conf = new ClientConfiguration();
ClientConfiguration.ConnectionLimit = 512;
conf.MaxErrorRetry = 3;
conf.ConnectionTimeout = 300;
conf.EnalbeMD5Check = true;
conf.ProxyHost = "example.aliyundoc.com";
conf.ProxyPort = 3128;
conf.ProxyUserName = "user";
conf.ProxyPassword = "password";
var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);