tencent cloud

Getting Started
Last updated: 2026-01-05 17:34:58
Getting Started
Last updated: 2026-01-05 17:34:58

Relevant Resources

Download address for the source code of the COS XML Python SDK: XML Python SDK.
Quick download address for SDK: XML Python SDK.
Download address of the Demo: XML Python Demo.
For all sample code in the SDK documentation, see SDK Code Samples.
For the change log of the SDK, see ChangeLog.
For SDK FAQs, see: Python SDK FAQs.
Note:
If you encounter errors such as functions or methods not found when using the XML version of the SDK, please first upgrade the XML version of the SDK to the latest version and try again.

Environment Configuration and Preparation

The XML Python SDK of COS currently supports Python 2.7 and Python 3.4 and above.
Note:
For the meanings of terms such as SecretId, SecretKey, Bucket, and Region mentioned in the article and how to obtain them, see COS Terminology.

Installing the SDK

There are three methods to install the SDK: pip installation, manual installation, and offline installation.

Method 1: Use pip to Install (Recommended)

pip install -U cos-python-sdk-v5

Method 2: Manual Installation

Download the source code from XML Python SDK and install it manually via setup. Run the following command:
python setup.py install

Method Three: Offline Installation

# Run the following command on a machine with external network.
mkdir cos-python-sdk-packages
pip download cos-python-sdk-v5 -d cos-python-sdk-packages
tar -czvf cos-python-sdk-packages.tar.gz cos-python-sdk-packages
# Copy the installation package to a machine without external network and run the following command.
# Ensure that the Python versions on both machines are consistent; otherwise, the installation may fail.
tar -xzvf cos-python-sdk-packages.tar.gz
pip install cos-python-sdk-v5 --no-index -f cos-python-sdk-packages

Initialize COS Service

The following describes how to perform basic operations after the client is initialized using the COS Python SDK, such as creating a bucket, listing buckets, uploading objects, listing objects, downloading objects, and deleting objects.

Initialize COS

Note:
It is recommended that users use sub-account keys + environment variables to call the SDK to enhance the security of SDK usage. When sub-accounts are authorized, follow the least privilege principle to prevent the leakage of resources other than the target bucket or objects.
If you must use permanent secret keys, it is recommended that you follow the Principle of Least Privilege to restrict the scope of permissions for these keys.
The following describes several methods to initialize the Client of the Python SDK. You can choose one based on your specific scenario. By default, access is initialized using the COS default domain.

Initialize Through the Default Domain

Initialization via the COS Default Domain Name
Initialization via the COS Default Domain and Temporary Key
COS Default Domain and Initializing the Proxy
When accessing COS via the default domain, the SDK accesses COS in the format {bucket-appid}.cos.{region}.myqcloud.com.
Note:
Under normal circumstances, you should create only one instance of CosS3Client per region and reuse it for looping uploads or downloads of objects. Do not create a new CosS3Client instance for each access, as this may cause the Python process to occupy excessive connections and threads.
# -*- coding=utf-8
from qcloud_cos import CosConfig
from qcloud_cos import CosS3Client
import sys
import os
import logging

# Under normal circumstances, use the INFO log level. When troubleshooting is required, you may change it to DEBUG, in which case the SDK will print communication information with the server.
logging.basicConfig(level=logging.INFO, stream=sys.stdout)

# 1. Set user attributes, including secret_id, secret_key, region, and so on. Appid has been removed from CosConfig; please include Appid in the Bucket parameter. Bucket is composed of BucketName-Appid.
secret_id = os.environ['COS_SECRET_ID'] # User's SecretId. It is recommended to use sub-account keys, with authorization following the principle of least privilege to reduce usage risks. For how to obtain sub-account keys, refer to https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1.
secret_key = os.environ['COS_SECRET_KEY'] # User's SecretKey. It is recommended to use sub-account keys, with authorization following the principle of least privilege to reduce usage risks. For how to obtain sub-account keys, refer to https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1.
region = 'ap-beijing' # Replace with the user's region. The region to which the created bucket belongs can be viewed in the console at https://console.tencentcloud.com/cos5/bucket.
# For a list of all regions supported by COS, see https://www.tencentcloud.com/document/product/436/6224?from_cn_redirect=1
token = None # For permanent keys, do not fill in the token. For temporary keys, fill in the token. Refer to https://www.tencentcloud.com/document/product/436/14048 for the generation and usage guide of temporary keys.
scheme = 'https' # Specify the protocol to access COS, either http/https. Default is https, which can be omitted.

config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token, Scheme=scheme)
client = CosS3Client(config)
Note:
For how to generate and use temporary keys, see Temporary Key Generation and Usage Guide.
When accessing COS via the default domain, the SDK accesses COS in the format {bucket-appid}.cos.{region}.myqcloud.com.
# -*- coding=utf-8
from qcloud_cos import CosConfig
from qcloud_cos import CosS3Client
import sys
import os
import logging

# Under normal circumstances, use the INFO log level. When troubleshooting is required, you may change it to DEBUG, in which case the SDK will print communication information with the server.
logging.basicConfig(level=logging.INFO, stream=sys.stdout)

# 1. Set user attributes, including secret_id, secret_key, region, and so on. Appid has been removed from CosConfig; please include Appid in the Bucket parameter. Bucket is composed of BucketName-Appid.
tmp_secret_id = 'TmpSecretId' # SecretId for the temporary key. For the generation and usage guide of temporary keys, refer to https://www.tencentcloud.com/document/product/436/14048.
tmp_secret_key = 'TmpSecretKey' # SecretKey for the temporary key. For the generation and usage guide of temporary keys, refer to https://www.tencentcloud.com/document/product/436/14048.
token = 'TmpToken' # Token for the temporary key. For the generation and usage guide of temporary keys, refer to https://www.tencentcloud.com/document/product/436/14048.
region = 'ap-beijing' # Replace with the user's region. The region to which the created bucket belongs can be viewed in the console at https://console.tencentcloud.com/cos5/bucket.
# For a list of all regions supported by COS, see https://www.tencentcloud.com/document/product/436/6224?from_cn_redirect=1
scheme = 'https' # Specify the protocol to access COS, either http/https. Default is https, which can be omitted.

config = CosConfig(Region=region, SecretId=tmp_secret_id, SecretKey=tmp_secret_key, Token=token, Scheme=scheme)
client = CosS3Client(config)
When proxy access to COS is required, configure it; otherwise, skip.
# -*- coding=utf-8
from qcloud_cos import CosConfig
from qcloud_cos import CosS3Client
import sys
import os
import logging

# Under normal circumstances, use the INFO log level. When troubleshooting is required, you may change it to DEBUG, in which case the SDK will print communication information with the server.
logging.basicConfig(level=logging.INFO, stream=sys.stdout)

# 1. Set user attributes, including secret_id, secret_key, region, and so on. Appid has been removed from CosConfig; please include Appid in the Bucket parameter. Bucket is composed of BucketName-Appid.
secret_id = os.environ['COS_SECRET_ID'] # User's SecretId. It is recommended to use sub-account keys, with authorization following the principle of least privilege to reduce usage risks. For how to obtain sub-account keys, refer to https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1.
secret_key = os.environ['COS_SECRET_KEY'] # User's SecretKey. It is recommended to use sub-account keys, with authorization following the principle of least privilege to reduce usage risks. For how to obtain sub-account keys, refer to https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1.
region = 'ap-beijing' # Replace with the user's region. The region to which the created bucket belongs can be viewed in the console at https://console.tencentcloud.com/cos5/bucket.
# For a list of all regions supported by COS, see https://www.tencentcloud.com/document/product/436/6224?from_cn_redirect=1
token = None # For permanent keys, do not fill in the token. For temporary keys, fill in the token. Refer to https://www.tencentcloud.com/document/product/436/14048 for the generation and usage guide of temporary keys.
proxies = {
'http': '127.0.0.1:80', # Replace with the user's proxy address for HTTP
'https': '127.0.0.1:443' # Replace with the user's proxy address for HTTPS
}

config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token, Proxies=proxies)
client = CosS3Client(config)

Initialize Via Other Domains

Domain Name for Global Acceleration
Custom Origin Domain
Default CDN Accelerated Domain
Custom Domain for CDN Acceleration
When the global accelerated domain is used to access COS, the SDK accesses COS in the format {bucket-appid}.cos.accelerate.myqcloud.com, and the region will not appear in the access domain.
# -*- coding=utf-8
from qcloud_cos import CosConfig
from qcloud_cos import CosS3Client
import sys
import os
import logging

# Under normal circumstances, use the INFO log level. When troubleshooting is required, you may change it to DEBUG, in which case the SDK will print communication information with the server.
logging.basicConfig(level=logging.INFO, stream=sys.stdout)

# 1. Set user attributes, including secret_id, secret_key, region, and so on. Appid has been removed from CosConfig; please include Appid in the Bucket parameter. Bucket is composed of BucketName-Appid.
secret_id = os.environ['COS_SECRET_ID'] # User's SecretId. It is recommended to use sub-account keys, with authorization following the principle of least privilege to reduce usage risks. For how to obtain sub-account keys, refer to https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1.
secret_key = os.environ['COS_SECRET_KEY'] # User's SecretKey. It is recommended to use sub-account keys, with authorization following the principle of least privilege to reduce usage risks. For how to obtain sub-account keys, refer to https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1.
region = None # When initialization is performed via Endpoint, there is no need to configure the region
token = None # For permanent keys, do not fill in the token. For temporary keys, fill in the token. Refer to https://www.tencentcloud.com/document/product/436/14048 for the generation and usage guide of temporary keys.
scheme = 'https' # Specify the protocol to access COS, either http/https. Default is https, which can be omitted.

endpoint = 'cos.accelerate.myqcloud.com' # Replace with the user's endpoint or cos global acceleration domain. To use a bucket's global acceleration domain, you must first enable the bucket's global acceleration feature. See https://www.tencentcloud.com/document/product/436/38864?from_cn_redirect=1.
config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token, Endpoint=endpoint, Scheme=scheme)
client = CosS3Client(config)
When access is made via a custom domain, the SDK will use the configured user domain to directly access COS. Neither the bucket nor the region will appear in the access domain.
# -*- coding=utf-8
from qcloud_cos import CosConfig
from qcloud_cos import CosS3Client
import sys
import os
import logging

# Under normal circumstances, use the INFO log level. When troubleshooting is required, you may change it to DEBUG, in which case the SDK will print communication information with the server.
logging.basicConfig(level=logging.INFO, stream=sys.stdout)

# 1. Set user attributes, including secret_id, secret_key, region, and so on. Appid has been removed from CosConfig; please include Appid in the Bucket parameter. Bucket is composed of BucketName-Appid.
secret_id = os.environ['COS_SECRET_ID'] # User's SecretId. It is recommended to use sub-account keys, with authorization following the principle of least privilege to reduce usage risks. For how to obtain sub-account keys, refer to https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1.
secret_key = os.environ['COS_SECRET_KEY'] # User's SecretKey. It is recommended to use sub-account keys, with authorization following the principle of least privilege to reduce usage risks. For how to obtain sub-account keys, refer to https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1.
region = None # When initialization is performed via a custom domain, there is no need to configure the region
token = None # For permanent keys, do not fill in the token. For temporary keys, fill in the token. Refer to https://www.tencentcloud.com/document/product/436/14048 for the generation and usage guide of temporary keys.
scheme = 'https' # Specify the protocol to access COS, either http/https. Default is https, which can be omitted.

domain = 'user-define.example.com' # User-defined domain. You need to enable the custom domain for the bucket first. For details, see https://www.tencentcloud.com/document/product/436/36638?from_cn_redirect=1.
config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token, Domain=domain, Scheme=scheme)
client = CosS3Client(config)
When the CDN default domain is accessed, the SDK accesses CDN in the format {bucket-appid}.file.myqcloud.com, and the content is fetched from the origin server by the CDN service.
# -*- coding=utf-8
from qcloud_cos import CosConfig
from qcloud_cos import CosS3Client
import sys
import os
import logging

# Under normal circumstances, use the INFO log level. When troubleshooting is required, you may change it to DEBUG, in which case the SDK will print communication information with the server.
logging.basicConfig(level=logging.INFO, stream=sys.stdout)

# 1. Set user attributes, including secret_id, secret_key, region, and so on. Appid has been removed from CosConfig; please include Appid in the Bucket parameter. Bucket is composed of BucketName-Appid.
secret_id = os.environ['COS_SECRET_ID'] # User's SecretId. It is recommended to use sub-account keys, with authorization following the principle of least privilege to reduce usage risks. For how to obtain sub-account keys, refer to https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1.
secret_key = os.environ['COS_SECRET_KEY'] # User's SecretKey. It is recommended to use sub-account keys, with authorization following the principle of least privilege to reduce usage risks. For how to obtain sub-account keys, refer to https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1.
region = None # When initialization is performed via Endpoint, there is no need to configure the region
token = None # For permanent keys, do not fill in the token. For temporary keys, fill in the token. Refer to https://www.tencentcloud.com/document/product/436/14048 for the generation and usage guide of temporary keys.
scheme = 'https' # Specify the protocol to access COS, either http/https. Default is https, which can be omitted.

endpoint = 'file.myqcloud.com' # Replace with the user's CDN default acceleration domain. This requires enabling configuration for CDN acceleration. See https://www.tencentcloud.com/document/product/436/18670?from_cn_redirect=1
config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token, Endpoint=endpoint, Scheme=scheme)
client = CosS3Client(config)
When access is performed via a CDN custom domain, the SDK uses the configured user domain to directly access CDN. Neither the bucket nor the region will appear in the access domain, and the content is fetched from the origin server by the CDN service.
# -*- coding=utf-8
from qcloud_cos import CosConfig
from qcloud_cos import CosS3Client
import sys
import os
import logging

# Under normal circumstances, use the INFO log level. When troubleshooting is required, you may change it to DEBUG, in which case the SDK will print communication information with the server.
logging.basicConfig(level=logging.INFO, stream=sys.stdout)

# 1. Set user attributes, including secret_id, secret_key, region, and so on. Appid has been removed from CosConfig; please include Appid in the Bucket parameter. Bucket is composed of BucketName-Appid.
secret_id = os.environ['COS_SECRET_ID'] # User's SecretId. It is recommended to use sub-account keys, with authorization following the principle of least privilege to reduce usage risks. For how to obtain sub-account keys, refer to https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1.
secret_key = os.environ['COS_SECRET_KEY'] # User's SecretKey. It is recommended to use sub-account keys, with authorization following the principle of least privilege to reduce usage risks. For how to obtain sub-account keys, refer to https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1.
region = None # When initialization is performed via a custom domain, there is no need to configure the region
token = None # For permanent keys, do not fill in the token. For temporary keys, fill in the token. Refer to https://www.tencentcloud.com/document/product/436/14048 for the generation and usage guide of temporary keys.
scheme = 'https' # Specify the protocol to access COS, either http/https. Default is https, which can be omitted.

domain = 'user-define.example-cdn.com' # User-defined CDN domain. This requires enabling acceleration for CDN custom domains. Refer to https://www.tencentcloud.com/document/product/436/18670?from_cn_redirect=1
config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token, Domain=domain, Scheme=scheme)
client = CosS3Client(config)

Access the COS Service

Create a bucket
Querying the Bucket List
PUT Object
Querying the Object List
Downloading an Object
Deleting Objects
# -*- coding=utf-8
from qcloud_cos import CosConfig
from qcloud_cos import CosS3Client
import sys
import os
import logging

# Under normal circumstances, use the INFO log level. When troubleshooting is required, you may change it to DEBUG, in which case the SDK will print communication information with the server.
logging.basicConfig(level=logging.INFO, stream=sys.stdout)

# 1. Set user attributes, including secret_id, secret_key, region, and so on. Appid has been removed from CosConfig; please include Appid in the Bucket parameter. Bucket is composed of BucketName-Appid.
secret_id = os.environ['COS_SECRET_ID'] # User's SecretId. It is recommended to use sub-account keys, with authorization following the principle of least privilege to reduce usage risks. For how to obtain sub-account keys, refer to https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1.
secret_key = os.environ['COS_SECRET_KEY'] # User's SecretKey. It is recommended to use sub-account keys, with authorization following the principle of least privilege to reduce usage risks. For how to obtain sub-account keys, refer to https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1.
region = 'ap-beijing' # Replace with the user's region. The region to which the created bucket belongs can be viewed in the console at https://console.tencentcloud.com/cos5/bucket.
# For a list of all regions supported by COS, see https://www.tencentcloud.com/document/product/436/6224?from_cn_redirect=1
token = None # For permanent keys, do not fill in the token. For temporary keys, fill in the token. Refer to https://www.tencentcloud.com/document/product/436/14048 for the generation and usage guide of temporary keys.
scheme = 'https' # Specify the protocol to access COS, either http/https. Default is https, which can be omitted.

config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token, Scheme=scheme)
client = CosS3Client(config)

response = client.create_bucket(
Bucket='examplebucket-1250000000'
)
# -*- coding=utf-8
from qcloud_cos import CosConfig
from qcloud_cos import CosS3Client
import sys
import os
import logging

# Under normal circumstances, use the INFO log level. When troubleshooting is required, you may change it to DEBUG, in which case the SDK will print communication information with the server.
logging.basicConfig(level=logging.INFO, stream=sys.stdout)

# 1. Set user attributes, including secret_id, secret_key, region, and so on. Appid has been removed from CosConfig; please include Appid in the Bucket parameter. Bucket is composed of BucketName-Appid.
secret_id = os.environ['COS_SECRET_ID'] # User's SecretId. It is recommended to use sub-account keys, with authorization following the principle of least privilege to reduce usage risks. For how to obtain sub-account keys, refer to https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1.
secret_key = os.environ['COS_SECRET_KEY'] # User's SecretKey. It is recommended to use sub-account keys, with authorization following the principle of least privilege to reduce usage risks. For how to obtain sub-account keys, refer to https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1.
region = 'ap-beijing' # Replace with the user's region. The region to which the created bucket belongs can be viewed in the console at https://console.tencentcloud.com/cos5/bucket.
# For a list of all regions supported by COS, see https://www.tencentcloud.com/document/product/436/6224?from_cn_redirect=1
token = None # For permanent keys, do not fill in the token. For temporary keys, fill in the token. Refer to https://www.tencentcloud.com/document/product/436/14048 for the generation and usage guide of temporary keys.
scheme = 'https' # Specify the protocol to access COS, either http/https. Default is https, which can be omitted.

config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token, Scheme=scheme)
client = CosS3Client(config)

response = client.list_buckets(
)
Note:
Simple upload does not support files larger than 5G. It is recommended to use the advanced upload interface below. For parameter descriptions, refer to the Object Operations documentation.
# -*- coding=utf-8
from qcloud_cos import CosConfig
from qcloud_cos import CosS3Client
import sys
import os
import logging

# Under normal circumstances, use the INFO log level. When troubleshooting is required, you may change it to DEBUG, in which case the SDK will print communication information with the server.
logging.basicConfig(level=logging.INFO, stream=sys.stdout)

# 1. Set user attributes, including secret_id, secret_key, region, and so on. Appid has been removed from CosConfig; please include Appid in the Bucket parameter. Bucket is composed of BucketName-Appid.
secret_id = os.environ['COS_SECRET_ID'] # User's SecretId. It is recommended to use sub-account keys, with authorization following the principle of least privilege to reduce usage risks. For how to obtain sub-account keys, refer to https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1.
secret_key = os.environ['COS_SECRET_KEY'] # User's SecretKey. It is recommended to use sub-account keys, with authorization following the principle of least privilege to reduce usage risks. For how to obtain sub-account keys, refer to https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1.
region = 'ap-beijing' # Replace with the user's region. The region to which the created bucket belongs can be viewed in the console at https://console.tencentcloud.com/cos5/bucket.
# For a list of all regions supported by COS, see https://www.tencentcloud.com/document/product/436/6224?from_cn_redirect=1
token = None # For permanent keys, do not fill in the token. For temporary keys, fill in the token. Refer to https://www.tencentcloud.com/document/product/436/14048 for the generation and usage guide of temporary keys.
scheme = 'https' # Specify the protocol to access COS, either http/https. Default is https, which can be omitted.

config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token, Scheme=scheme)
client = CosS3Client(config)

#### Simple file-stream upload (does not support files larger than 5G; it is recommended to use the advanced upload interface below)
# It is strongly recommended to open the file in binary mode; otherwise, errors may occur.
with open('picture.jpg', 'rb') as fp:
response = client.put_object(
Bucket='examplebucket-1250000000',
Body=fp,
Key='picture.jpg',
StorageClass='STANDARD',
EnableMD5=False
)
print(response['ETag'])

#### Simple upload of byte streams
response = client.put_object(
Bucket='examplebucket-1250000000',
Body=b'bytes',
Key='picture.jpg',
EnableMD5=False
)
print(response['ETag'])


#### Simple upload of chunks
import requests
stream = requests.get('https://www.tencentcloud.com/document/product/436/7778?from_cn_redirect=1')

# Network streams are transmitted to COS in `Transfer-Encoding:chunked` mode.
response = client.put_object(
Bucket='examplebucket-1250000000',
Body=stream,
Key='picture.jpg'
)
print(response['ETag'])

#### Advanced upload API (recommended)
# Automatically selects simple upload or multipart upload based on file size. Multipart upload supports the feature for resumable upload.
response = client.upload_file(
Bucket='examplebucket-1250000000',
LocalFilePath='local.txt',
Key='picture.jpg',
PartSize=1,
MAXThread=10,
EnableMD5=False
)
print(response['ETag'])
# -*- coding=utf-8
from qcloud_cos import CosConfig
from qcloud_cos import CosS3Client
import sys
import os
import logging

# Under normal circumstances, use the INFO log level. When troubleshooting is required, you may change it to DEBUG, in which case the SDK will print communication information with the server.
logging.basicConfig(level=logging.INFO, stream=sys.stdout)

# 1. Set user attributes, including secret_id, secret_key, region, and so on. Appid has been removed from CosConfig; please include Appid in the Bucket parameter. Bucket is composed of BucketName-Appid.
secret_id = os.environ['COS_SECRET_ID'] # User's SecretId. It is recommended to use sub-account keys, with authorization following the principle of least privilege to reduce usage risks. For how to obtain sub-account keys, refer to https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1.
secret_key = os.environ['COS_SECRET_KEY'] # User's SecretKey. It is recommended to use sub-account keys, with authorization following the principle of least privilege to reduce usage risks. For how to obtain sub-account keys, refer to https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1.
region = 'ap-beijing' # Replace with the user's region. The region to which the created bucket belongs can be viewed in the console at https://console.tencentcloud.com/cos5/bucket.
# For a list of all regions supported by COS, see https://www.tencentcloud.com/document/product/436/6224?from_cn_redirect=1
token = None # For permanent keys, do not fill in the token. For temporary keys, fill in the token. Refer to https://www.tencentcloud.com/document/product/436/14048 for the generation and usage guide of temporary keys.
scheme = 'https' # Specify the protocol to access COS, either http/https. Default is https, which can be omitted.

config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token, Scheme=scheme)
client = CosS3Client(config)

response = client.list_objects(
Bucket='examplebucket-1250000000',
Prefix='folder1'
)
A single call to the list_objects API can only retrieve 1000 objects. If you need to query all objects, you must make repeated calls.
# -*- coding=utf-8
from qcloud_cos import CosConfig
from qcloud_cos import CosS3Client
import sys
import os
import logging

# Under normal circumstances, use the INFO log level. When troubleshooting is required, you may change it to DEBUG, in which case the SDK will print communication information with the server.
logging.basicConfig(level=logging.INFO, stream=sys.stdout)

# 1. Set user attributes, including secret_id, secret_key, region, and so on. Appid has been removed from CosConfig; please include Appid in the Bucket parameter. Bucket is composed of BucketName-Appid.
secret_id = os.environ['COS_SECRET_ID'] # User's SecretId. It is recommended to use sub-account keys, with authorization following the principle of least privilege to reduce usage risks. For how to obtain sub-account keys, refer to https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1.
secret_key = os.environ['COS_SECRET_KEY'] # User's SecretKey. It is recommended to use sub-account keys, with authorization following the principle of least privilege to reduce usage risks. For how to obtain sub-account keys, refer to https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1.
region = 'ap-beijing' # Replace with the user's region. The region to which the created bucket belongs can be viewed in the console at https://console.tencentcloud.com/cos5/bucket.
# For a list of all regions supported by COS, see https://www.tencentcloud.com/document/product/436/6224?from_cn_redirect=1
token = None # For permanent keys, do not fill in the token. For temporary keys, fill in the token. Refer to https://www.tencentcloud.com/document/product/436/14048 for the generation and usage guide of temporary keys.
scheme = 'https' # Specify the protocol to access COS, either http/https. Default is https, which can be omitted.

config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token, Scheme=scheme)
client = CosS3Client(config)

marker = ""
while True:
response = client.list_objects(
Bucket='examplebucket-1250000000',
Prefix='folder1',
Marker=marker
)
if 'Contents' in response:
print(response['Contents'])
if response['IsTruncated'] == 'false':
break
marker = response['NextMarker']
# -*- coding=utf-8
from qcloud_cos import CosConfig
from qcloud_cos import CosS3Client
import sys
import os
import logging

# Under normal circumstances, use the INFO log level. When troubleshooting is required, you may change it to DEBUG, in which case the SDK will print communication information with the server.
logging.basicConfig(level=logging.INFO, stream=sys.stdout)

# 1. Set user attributes, including secret_id, secret_key, region, and so on. Appid has been removed from CosConfig; please include Appid in the Bucket parameter. Bucket is composed of BucketName-Appid.
secret_id = os.environ['COS_SECRET_ID'] # User's SecretId. It is recommended to use sub-account keys, with authorization following the principle of least privilege to reduce usage risks. For how to obtain sub-account keys, refer to https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1.
secret_key = os.environ['COS_SECRET_KEY'] # User's SecretKey. It is recommended to use sub-account keys, with authorization following the principle of least privilege to reduce usage risks. For how to obtain sub-account keys, refer to https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1.
region = 'ap-beijing' # Replace with the user's region. The region to which the created bucket belongs can be viewed in the console at https://console.tencentcloud.com/cos5/bucket.
# For a list of all regions supported by COS, see https://www.tencentcloud.com/document/product/436/6224?from_cn_redirect=1
token = None # For permanent keys, do not fill in the token. For temporary keys, fill in the token. Refer to https://www.tencentcloud.com/document/product/436/14048 for the generation and usage guide of temporary keys.
scheme = 'https' # Specify the protocol to access COS, either http/https. Default is https, which can be omitted.

config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token, Scheme=scheme)
client = CosS3Client(config)

#### Download files to a local directory.
response = client.get_object(
Bucket='examplebucket-1250000000',
Key='picture.jpg'
)
response['Body'].get_stream_to_file('output.txt')

#### Obtain file streams.
response = client.get_object(
Bucket='examplebucket-1250000000',
Key='picture.jpg'
)
fp = response['Body'].get_raw_stream()
print(fp.read(2))

#### Set the HTTP header in the response.
response = client.get_object(
Bucket='examplebucket-1250000000',
Key='picture.jpg',
ResponseContentType='text/html; charset=utf-8'
)
print(response['Content-Type'])
fp = response['Body'].get_raw_stream()
print(fp.read(2))

#### Specify the download range.
response = client.get_object(
Bucket='examplebucket-1250000000',
Key='picture.jpg',
Range='bytes=0-10'
)
fp = response['Body'].get_raw_stream()
print(fp.read())
Note:
Once an object is deleted, its corresponding data will no longer be accessible.
# -*- coding=utf-8
from qcloud_cos import CosConfig
from qcloud_cos import CosS3Client
import sys
import os
import logging

# Under normal circumstances, use the INFO log level. When troubleshooting is required, you may change it to DEBUG, in which case the SDK will print communication information with the server.
logging.basicConfig(level=logging.INFO, stream=sys.stdout)

# 1. Set user attributes, including secret_id, secret_key, region, and so on. Appid has been removed from CosConfig; please include Appid in the Bucket parameter. Bucket is composed of BucketName-Appid.
secret_id = os.environ['COS_SECRET_ID'] # User's SecretId. It is recommended to use sub-account keys, with authorization following the principle of least privilege to reduce usage risks. For how to obtain sub-account keys, refer to https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1.
secret_key = os.environ['COS_SECRET_KEY'] # User's SecretKey. It is recommended to use sub-account keys, with authorization following the principle of least privilege to reduce usage risks. For how to obtain sub-account keys, refer to https://www.tencentcloud.com/document/product/598/37140?from_cn_redirect=1.
region = 'ap-beijing' # Replace with the user's region. The region to which the created bucket belongs can be viewed in the console at https://console.tencentcloud.com/cos5/bucket.
# For a list of all regions supported by COS, see https://www.tencentcloud.com/document/product/436/6224?from_cn_redirect=1
token = None # For permanent keys, do not fill in the token. For temporary keys, fill in the token. Refer to https://www.tencentcloud.com/document/product/436/14048 for the generation and usage guide of temporary keys.
scheme = 'https' # Specify the protocol to access COS, either http/https. Default is https, which can be omitted.

config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token, Scheme=scheme)
client = CosS3Client(config)

# Delete an object.
## deleteObject
response = client.delete_object(
Bucket='examplebucket-1250000000',
Key='exampleobject'
)

# Delete multiple objects.
## deleteObjects
response = client.delete_objects(
Bucket='examplebucket-1250000000',
Delete={
'Object': [
{
'Key': 'exampleobject1'
},
{
'Key': 'exampleobject2'
},
],
'Quiet': 'true' # Specifies the method for the response to deletion. Valid values: true, false
}
)

FAQs

You may encounter some common issues during use. For related solutions, see Python SDK FAQs.
Was this page helpful?
You can also Contact Sales or Submit a Ticket for help.
Yes
No

Feedback