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

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

# -*- 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 Resource Access Management (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. 
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 objects

The following code provides an example on how to upload a local file to OSS:

# -*- 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. 
bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', '<yourBucketName>')

# <yourObjectName> indicates the full path of the object you want to upload to OSS, and must include the extension of the object 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 objects

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. 
bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', '<yourBucketName>')
# <yourObjectName> indicates the full path of the object you want to download from OSS, and must include the extension of the object name. Example: abc/efg/123.jpg. 
# <yourLocalFile> consists of a local file path and an object name with an 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. 
bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', '<yourBucketName>')

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

Delete objects

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. 
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, and must include the extension of the object name. Example: abc/efg/123.jpg. 
bucket.delete_object('<yourObjectName>')
            

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