Initialization

Last Updated: Aug 07, 2017

The OSS\OSSClient is a client class of the SDK, which provides a series of interfaces to manage buckets and objects.

Determine an endpoint

An endpoint is the address 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 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.

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:

  • Query the mapping relationship between the endpoint and the region. For details, click to view details.
  • 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 an access key

To access Alibaba Cloud OSS, you need a valid pair of access keys (an AccesskeyID and an AccessKeySecret) for signature authentication. The access keys can be obtained via the steps below:

After obtaining the AccessKeyId and AccessKeySecret, you can follow the following steps for initialization.

Create an OSSClient

Use an OSS domain to create an OSSClient

  1. <?php
  2. use OSS\OssClient;
  3. use OSS\Core\OssException;
  4. $accessKeyId = "<AccessKeyID that you obtain from OSS>";
  5. $accessKeySecret = "<AccessKeySecret that you obtain from OSS>";
  6. $endpoint = "<Domain that you select to access an OSS data center, such as http://oss-cn-hangzhou.aliyuncs.com>";
  7. try {
  8. $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
  9. } catch (OssException $e) {
  10. print $e->getMessage();
  11. }

For the list of all nodes of OSS, refer to OSS Node List.

Use a custom domain name (CNAME) to create an OSSClient

  1. <?php
  2. use OSS\OssClient;
  3. use OSS\Core\OssException;
  4. $accessKeyId = "<AccessKeyID that you obtain from OSS>";
  5. $accessKeySecret = "<AccessKeySecret that you obtain from OSS>";
  6. $endpoint = "<your custom domain name bound to a bucket>";
  7. try {
  8. $ossClient = new OssClient(
  9. $accessKeyId, $accessKeySecret, $endpoint, true /* use cname */);
  10. } catch (OssException $e) {
  11. print $e->getMessage();
  12. }

In the OssClient constructor, the fourth parameter indicates whether to use the custom domain name. This parameter must be set to true when the CNAME is used. If you use an official OSS domain, you do not need to set the parameter or set it to false.

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

Set network parameters

We can use ClientConfiguration to set some network parameters:

  1. <?php
  2. $ossClient->setTimeout(3600 /* seconds */);
  3. $ossClient->setConnectTimeout(10 /* seconds */);

Specifically:

  • The setTimeout parameter indicates the request timeout time in the unit of seconds. The default value is 5,184,000 seconds. We recommend that the parameter be set to a big value. Otherwise, it will take a long time to upload a large file.
  • The setConnectTimeout parameter indicates the connection timeout time in the unit of seconds. The default value is 10 seconds.
Thank you! We've received your feedback.