Initialization

Last Updated: Jul 25, 2017

The OSSClient is the Android client of OSS. It provides the caller with a series of methods to operate and manage the buckets and objects. Before you use the OSS Android SDK to initiate a request to OSS, you need to first initialize an OSSClient instance and complete necessary settings.

Determine an endpoint

An endpoint is the address of Alibaba Cloud OSS in a region. It supports the following two formats:

Endpoint type Description
OSS region address Address of the region where an OSS bucket is located. For details about the endpoints in various regions, refer to Regions and endpoints.
User-defined domain name Domain name defined by the user, with the CNAME directing to the OSS domain.

For details about endpoints, click to view details.

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 details, click to view details.

  • 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 the CNAME and access the objects in the bucket through the domain.

  • For example, if you need 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 the endpoint and credential

A mobile terminal is not a trusted environment. There is an extremely high risk if you store the AccessKeyID and AccessKeySecret in a mobile terminal for signing requests. We recommend that you use STS Authentication Mode or Self-signed Mode. For details, refer to Access control and Application server. An example of EndPoint and CredentialProvider settings is shown below:

  1. String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
  2. // AccessKeySecret setting in plain text mode is recommended for test purposes only. For more authentication modes, refer to the "Resource Access Control" section
  3. OSSCredentialProvider credentialProvider = new OSSStsTokenCredentialProvider("<StsToken.AccessKeyId>", "<StsToken.SecretKeyId>", "<StsToken.SecurityToken>");
  4. OSS oss = new OSSClient(getApplicationContext(), endpoint, credentialProvider);

Note: For more authentication modes, refer to Access control.

Configure the endpoint according to the CNAME

If you have bound the CNAME to the bucket, you can configure the endpoint the same as the CNAME. For example:

  1. String endpoint = "http://new-image.xxxxx.com";
  2. ...
  3. OSS oss = new OSSClient(getApplicationContext(), endpoint, credentialProvider);

Note: For more authentication modes, refer to Access control.

Set network parameters

You can set detailed ClientConfiguration during the initialization process as follows:

  1. String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
  2. // We recommend that you initialize OSSClient using STS on the mobile terminal. For more details, refer to "Access Control"
  3. OSSCredentialProvider credentialProvider = new OSSStsTokenCredentialProvider("<StsToken.AccessKeyId>", "<StsToken.SecretKeyId>", "<StsToken.SecurityToken>");
  4. ClientConfiguration conf = new ClientConfiguration();
  5. conf.setConnectionTimeout(15 * 1000); // The connection timed out. The default timeout value is 15 seconds
  6. conf.setSocketTimeout(15 * 1000); // The socket timed out. The default timeout value is 15 seconds.
  7. conf.setMaxConcurrentRequest(5); // The maximum number of concurrent requests. The default value is 5
  8. conf.setMaxErrorRetry(2); // The maximum number of retries after a failed request. The default value is 2
  9. OSS oss = new OSSClient(getApplicationContext(), endpoint, credentialProvider, conf);
Thank you! We've received your feedback.