OssClient serves as the OSS .NET client 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:
Use an endpoint to create an OssClient instance
The following code provides an example on how to create an OssClient instance based on an OSS endpoint:
using Aliyun.OSS;
// The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console.
const string accessKeyId = "yourAccessKeyId";
const string accessKeySecret = "yourAccessKeySecret";
// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
const string endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// Create an OssClient instance.
var ossClient = new OssClient(endpoint, accessKeyId, accessKeySecret);
Use a custom domain name to create an OssClient instance
The following code provides an example on how to create an OssClient instance by using a custom domain name:
using Aliyun.OSS;
using Aliyun.OSS.Common;
// The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console.
const string accessKeyId = "yourAccessKeyId";
const string accessKeySecret = "yourAccessKeySecret";
// Specify the custom domain name.
const string endpoint = "yourDomain";
// Create a ClientConfiguration instance. Modify parameters as required.
var conf = new ClientConfiguration();
// Specify that a CNAME can be used as an endpoint to create the OssClient instance. A CNAME record specifies the mapping relationship between a custom domain name and a bucket.
conf.IsCname = true;
// Create an OSSClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);
Use STS to create an OssClient instance
The following code provides an example on how to create an OssClient instance based on Security Token Service (STS):
using Aliyun.OSS;
// Specify the temporary AccessKey pair obtained from STS.
const string accessKeyId = "yourAccessKeyId";
const string accessKeySecret = "yourAccessKeySecret";
// Specify the security token obtained from STS.
const string securityToken = "yourSecurityToken";
// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
const string endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// Create an OssClient instance.
var ossClient = new OssClient(endpoint, accessKeyId, accessKeySecret, securityToken);
Configure an OssClient instance
ClientConfiguration is a configuration class of OssClient. You can use ClientConfiguration to configure parameters such as host proxies, connection timeouts, and the maximum number of connections. You can configure the following parameters.
Parameter | Description | Default value |
---|---|---|
ConnectionLimit | The maximum number of concurrent connections. | 512 |
MaxErrorRetry | The maximum number of retry attempts in the case of a request error. | 3 |
ConnectionTimeout | The timeout period for connections. Unit: milliseconds. | -1 (no timeout) |
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 address of the proxy server. Example: example.aliyundoc.com . | None |
ProxyPort | The port of the proxy server. Example: 3128 or 8080 . | None |
ProxyUserName | The account used to log on to the proxy server. This parameter is optional. | None |
ProxyPassword | The password used to log on to the proxy server. This parameter is optional. | None |
The following code provides an example on how to configure an OssClient instance:
using Aliyun.OSS;
using Aliyun.OSS.Common;
var conf = new ClientConfiguration();
// Specify the maximum number of concurrent connections.
ClientConfiguration.ConnectionLimit = 512;
// Specify the maximum number of retry attempts in the case of a request error.
conf.MaxErrorRetry = 3;
// Specify the timeout period for connections.
conf.ConnectionTimeout = 300;
// Enable MD5 verification.
conf.EnalbeMD5Check = true;
// Specify the proxy server.
conf.ProxyHost = "example.aliyundoc.com";
// Specify the port of the proxy server.
conf.ProxyPort = 3128;
// Specify the username used to access the proxy network.
conf.ProxyUserName = "user";
// Specify the password used to access the proxy network.
conf.ProxyPassword = "password";
var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);