edit-icon download-icon

Initialization

Last Updated: Dec 22, 2017

The OSSClient is the C# client of OSS. It provides the caller with a series of methods to operate and manage buckets and objects.

Determine an endpoint

An endpoint is the domain of Alibaba Cloud OSS in a region. It currently supports two formats.

Endpoint type Description
OSS region address Address of the region where an OSS bucket is located. For more information, see Regions and endpoints.
User-defined domain name Domain name defined by the user, with the CNAME directing to the OSS domain

OSS region address

You can use one of the following two methods to search for an endpoint mapped to the address of the region where an OSS bucket is located:

  • You can query the mapping relationship between the endpoint and the region. For more information, see Regions and endpoints.

  • You can log on to Alibaba Cloud OSS Console, open the Bucket Overview page, and find the suffix of the bucket domain. For example, the suffix oss-cn-hangzhou.aliyuncs.com of the bucket domain bucket-1.oss-cn-hangzhou.aliyuncs.com is the endpoint of the bucket on the Internet.

CNAME

  • You can bind your domain to a bucket through CNAME and access the objects in the bucket through the domain.

  • For example, if you are required to bind the new-image.xxxxx.com domain to the bucket named “image” in Shenzhen, do the following:

Contact your domain hosting provider for xxxxx.com to configure a new domain name resolution record used to resolve http://new-image.xxxxx.com to http://image.oss-cn-shenzhen.aliyuncs.com. The record type is CNAME.

Configure an AccessKey

To access Alibaba Cloud OSS, you need a valid pair of AccessKeys (including an AccessKeyID and AccessKeySecret) for signature authentication. The steps to obtain AccessKeys are as follows:

The following are the steps to initialize an OSSClient instance using the AccessKeyId and AccessKeySecret:

Create a client by using the OSS domain

The following code creates an OSSClient:

  1. using Aliyun.OSS;
  2. const string accessKeyId = "<your AccessKeyId>";
  3. const string accessKeySecret = "<your AccessKeySecret>";
  4. const string endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
  5. /// <summary>
  6. /// Use the OSS access address specified by the user and the AccessKeyID and AccessKeySecret granted by Alibaba Cloud to create a new OSSClient instance.
  7. /// </summary>
  8. /// <param name="endpoint">OSS access address. </param>
  9. /// <param name="accessKeyId">OSS access ID. </param>
  10. /// <param name="accessKeySecret">OSS access password. </param>
  11. var ossClient = new OssClient(endpoint, accessKeyId, accessKeySecret);

Create a client using a custom domain name (CNAME)

The following code allows the client to access OSS using the CNAME:

  1. using Aliyun.OSS;
  2. using Aliyun.OSS.Common;
  3. // Create a ClientConfiguration instance
  4. var conf = new ClientConfiguration();
  5. // Use CNAME for the configuration
  6. conf.IsCname = true;
  7. /// <summary>
  8. /// Use the OSS access address specified by the user, the AccessKeyID and AccessKeySecret granted by Alibaba Cloud, and client configuration
  9. /// to create an OSSClient instance.
  10. /// </summary>
  11. /// <param name="endpoint">OSS access address. </param>
  12. /// <param name="accessKeyId">OSS access ID. </param>
  13. /// <param name="accessKeySecret">OSS access password. </param>
  14. /// <param name="conf">Client configuration. </param>
  15. var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);

Note: The ListBuckets interface is unavailable when CNAME is used as the endpoint.

Configure the client

To configure detailed parameters for the OSSClient, you can import a ClientConfiguration object when creating the OSSClient. ClientConfiguration is the OSS service configuration class. It allows you to configure a proxy for the client, the maximum number of connections, and other parameters.

Set network parameters

We can use ClientConfiguration to set some network parameters:

  1. conf.ConnectionLimit = 512; //The maximum number of concurrent connections for HttpWebRequest
  2. conf.MaxErrorRetry = 3; //The maximum number of retries upon a setting request error
  3. conf.ConnectionTimeout = 300; //Set the connection time-out value
  4. conf.SetCustomEpochTicks(customEpochTicks); //Set the custom benchmark time
Thank you! We've received your feedback.