Initialization

Last Updated: Jun 01, 2017

Almost all operations to Python SDK are performed through oss2.Service and oss2.Bucket. The following details how to initialize the above two classes.

Determine an endpoint

First read the Access Domain Names and the Custom Access Domain sections in the Developer Guide to understand the concept of an endpoint.

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 via HTTP protocol
https://oss-cn-beijing.aliyuncs.com Access a bucket in the Beijing region through the internet via HTTPS protocol
http://my-domain.com Access a bucket through a custom domain name (CNAME) via HTTP protocol

For the CNAME, please be noted that:

  • 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://.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 timeout

You can specify the optional parameter ‘connect_timeout’ to set the connection timeout value in seconds. The following code initializes an oss2.Service object, and sets the connection timeout 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

CRC data verification is enabled by default for uploading and downloading objects to ensure data integrity during uploading and downloading. 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. To learn 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 shown as below:

  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.