Initialization

Last Updated: Dec 21, 2017

Almost all operations to Python SDK are performed through oss2.Service and oss2.Bucket. How to initialize the two classes is described as follow:

Determine an endpoint

First read the Regions and endpoints in the Developer Guide to understand the endpoint concept.

An endpoint may have the following forms:

Example Description
http://oss-cn-hangzhou.aliyuncs.com Access a bucket in the Hangzhou region through the Internet through HTTP protocol.
https://oss-cn-beijing.aliyuncs.com Access a bucket in the Beijing region through the Internet through HTTPS protocol.
http://my-domain.com Access a bucket through a custom domain name (CNAME) through HTTP protocol.

Note:

  • oss2.Service only supports non-CNAME endpoints.

  • You can bind a custom domain name (CNAME) my-domain.com to an OSS domain such as http://<my-bucket>.oss-cn-hangzhou.aliyuncs.com.

The following code sets an OSS access domain name as an endpoint parameter:

  1. # -*- coding: utf-8 -*-
  2. import oss2
  3. auth = oss2.Auth ('Your AccessKeyID', 'Your AccessKeySecret')
  4. endpoint = 'http://oss-cn-hangzhou.aliyuncs.com' # Suppose that your bucket is in Hangzhou region
  5. bucket = oss2.Bucket(auth, endpoint, 'your bucket name')

The following code allows you to use a custom domain name:

  1. # -*- coding: utf-8 -*-
  2. import oss2
  3. auth = oss2.Auth ('Your AccessKeyID', 'Your AccessKeySecret')
  4. cname = 'http://my-domain.com' # Suppose that your domain name is my-domain.com
  5. bucket = oss2.Bucket(auth, cname, 'your bucket name', is_cname=True)

Set connection time-out

You can specify the optional parameter connect_timeout to set the connection time-out value in seconds. The following code initializes an oss2.Service object, and sets the connection time-out value to 30 seconds:

  1. # -*- coding: utf-8 -*-
  2. import oss2
  3. auth = oss2.Auth ('Your AccessKeyID', 'Your AccessKeySecret')
  4. endpoint = 'http://oss-cn-hangzhou.aliyuncs.com' # Suppose that your bucket is in Hangzhou region
  5. bucket = oss2.Bucket(auth, endpoint, 'your bucket name', connect_timeout=30)

CRC data verification

By default, CRC data verification is enabled to upload and download the objects to be certain of data integrity during the upload and download process. CRC data verification uses the crcmod module to calculate the CRC64 of the transmitted data. If the C extension module of crcmod is not successfully installed, the efficiency of CRC64 calculation is poor.

How to determine whether the C extension mode has been installed, see Install SDK. If the C extension module of crcmod is not successfully installed, configure the compilation environment to re-install the module, or disable the CRC data verification feature. The method for disabling the CRC data verification feature is as follows:

  1. # -*- coding: utf-8 -*-
  2. import oss2
  3. auth = oss2.Auth ('Your AccessKeyID', 'Your AccessKeySecret')
  4. endpoint = 'http://oss-cn-hangzhou.aliyuncs.com' # Suppose that your bucket is in Hangzhou region
  5. bucket = oss2.Bucket(auth, endpoint, 'your bucket name', enable_crc=False)
Thank you! We've received your feedback.