This topic describes how to use Object Storage Service (OSS) SDK for Python to perform routine operations such as create buckets, upload objects, and download objects.

Create buckets

A bucket is a global namespace in OSS. A bucket is a container for objects stored in OSS.

Note For more information about endpoints, see Regions and endpoints. For more information about the naming conventions for buckets, see the "bucket" section in Terms.

The following code provides an example on how to create a bucket named examplebucket:

# -*- coding: utf-8 -*-
import oss2

# Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
auth = oss2.Auth('<yourAccessKeyId>', '<yourAccessKeySecret>')
# The endpoint of the China (Hangzhou) region is used in this region. Specify the actual endpoint based on your requirements. 
bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', '<yourBucketName>')

# Set the access control list (ACL) of the bucket to private. 
bucket.create_bucket(oss2.models.BUCKET_ACL_PRIVATE)

For more information about how to create buckets, see Create buckets.

Upload an object

The following code provides an example on how to upload objects to OSS by using stream upload:

# -*- coding: utf-8 -*-
import oss2

# Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
auth = oss2.Auth('<yourAccessKeyId>', '<yourAccessKeySecret>')
# The endpoint of the China (Hangzhou) region is used in this region. Specify the actual endpoint based on your requirements. 
bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', '<yourBucketName>')

# Upload the object to OSS. 
# <yourObjectName> indicates the full path of the object you want to upload to OSS. The full path of the object must include the extension of the object name and exclude the bucket name. Example: abc/efg/123.jpg. 
# <yourLocalFile> consists of a local file path and an object name with extension. Example: /users/local/myfile.txt. 
bucket.put_object_from_file('<yourObjectName>', '<yourLocalFile>')

Download an object

The following code provides an example on how to download a specified object to a local file:

# -*- coding: utf-8 -*-
import oss2

# Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
auth = oss2.Auth('<yourAccessKeyId>', '<yourAccessKeySecret>')
# The endpoint of the China (Hangzhou) region is used in this region. Specify the actual endpoint based on your requirements. 
bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', '<yourBucketName>')

# Download the object to a local file. 
# <yourObjectName> indicates the full path of the object you want to download from OSS. The full path of the object must include the extension of the object name and exclude the bucket name. Example: abc/efg/123.jpg. 
# <yourLocalFile> consists of a local file path and an object name with extension. Example: /users/local/myfile.txt. 
bucket.get_object_to_file('<yourObjectName>', '<yourLocalFile>')

List objects

The following code provides an example on how to list 10 objects in a specified bucket:

# -*- coding: utf-8 -*-
import oss2
from itertools import islice

# Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
auth = oss2.Auth('<yourAccessKeyId>', '<yourAccessKeySecret>')
# The endpoint of the China (Hangzhou) region is used in this region. Specify the actual endpoint based on your requirements. 
bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', '<yourBucketName>')

# oss2.ObjectIteratorr is used to traverse objects. 
for b in islice(oss2.ObjectIterator(bucket), 10):
    print(b.key)

Delete an object

The following code provides an example on how to delete an object:

# -*- coding: utf-8 -*-
import oss2

# Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
auth = oss2.Auth('<yourAccessKeyId>', '<yourAccessKeySecret>')
# The endpoint of the China (Hangzhou) region is used in this region. Specify the actual endpoint based on your requirements. 
bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', '<yourBucketName>')

# <yourObjectName> indicates the full path of the object you want to delete from OSS. The full path of the object must include the extension of the object name and exclude the bucket name. Example: abc/efg/123.jpg. 
bucket.delete_object('<yourObjectName>')

For more information about how to delete objects, see Delete objects in Manage objects.