pip install -U cos-python-sdk-v5
python setup.py install
# Run the following commands on a device that is connected to the Internetmkdir cos-python-sdk-packagespip download cos-python-sdk-v5 -d cos-python-sdk-packagestar -czvf cos-python-sdk-packages.tar.gz cos-python-sdk-packages# Copy the installation package to a device that is not connected to the Internet and run the following commands# Please make sure that the version of Python on the two computers is the same; otherwise, the installation will fail.tar -xzvf cos-python-sdk-packages.tar.gzpip install cos-python-sdk-v5 --no-index -f cos-python-sdk-packages
# -*- coding=utf-8from qcloud_cos import CosConfigfrom qcloud_cos import CosS3Clientimport sysimport logging# In most cases, set the log level to INFO. If you need to debug, you can set it to DEBUG and the SDK will print information about the communication with the server.logging.basicConfig(level=logging.INFO, stream=sys.stdout)# 1. Set user attributes such as secret_id, secret_key, and region. Appid has been removed from CosConfig and thus needs to be specified in Bucket, which is formatted as BucketName-Appid.secret_id = 'SecretId' # Replace it with the actual SecretId, which can be viewed and managed at https://console.tencentcloud.com/cam/capi.secret_key = 'SecretKey' # Replace it with the actual SecretKey, which can be viewed and managed at https://console.tencentcloud.com/cam/capi.region = 'ap-beijing' # Replace it with the actual region, which can be viewed in the console at https://console.tencentcloud.com/cos5/bucket.# For the list of regions supported by COS, see https://www.tencentcloud.com/document/product/436/6224?from_cn_redirect=1.token = None # Token is required for temporary keys but not permanent keys. For more information about how to generate and use a temporary key, visit https://www.tencentcloud.com/document/product/436/14048.scheme = 'https' # Specify whether to use HTTP or HTTPS protocol to access COS. This field is optional and is `https` by default.config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token, Scheme=scheme)client = CosS3Client(config)
# -*- coding=utf-8from qcloud_cos import CosConfigfrom qcloud_cos import CosS3Clientimport sysimport logging# In most cases, set the log level to INFO. If you need to debug, you can set it to DEBUG and the SDK will print information about the communication with the server.logging.basicConfig(level=logging.INFO, stream=sys.stdout)# 1. Set user attributes such as secret_id, secret_key, and region. Appid has been removed from CosConfig and thus needs to be specified in Bucket, which is formatted as BucketName-Appid.secret_id = 'SecretId' # Replace it with the actual SecretId, which can be viewed and managed at https://console.tencentcloud.com/cam/capi.secret_key = 'SecretKey' # Replace it with the actual SecretKey, which can be viewed and managed at https://console.tencentcloud.com/cam/capi.region = 'ap-beijing' # Replace it with the actual region, which can be viewed in the console at https://console.tencentcloud.com/cos5/bucket.# For the list of regions supported by COS, see https://www.tencentcloud.com/document/product/436/6224?from_cn_redirect=1.token = None # Token is required for temporary keys but not permanent keys. For more information about how to generate and use a temporary key, visit https://www.tencentcloud.com/document/product/436/14048.proxies = {'http': '127.0.0.1:80', # Replace it with your HTTP Proxy IP.'https': '127.0.0.1:443' # Replace it with your HTTPS Proxy IP.}config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token, Proxies=proxies)client = CosS3Client(config)
{bucket-appid}.cos.accelerate.myqcloud.com
. region
will not appear in the domain name.# -*- coding=utf-8from qcloud_cos import CosConfigfrom qcloud_cos import CosS3Clientimport sysimport logging# In most cases, set the log level to INFO. If you need to debug, you can set it to DEBUG and the SDK will print information about the communication with the server.logging.basicConfig(level=logging.INFO, stream=sys.stdout)# 1. Set user attributes such as secret_id, secret_key, and region. Appid has been removed from CosConfig and thus needs to be specified in Bucket, which is formatted as BucketName-Appid.secret_id = 'SecretId' # Replace it with the actual SecretId, which can be viewed and managed at https://console.tencentcloud.com/cam/capi.secret_key = 'SecretKey' # Replace it with the actual SecretKey, which can be viewed and managed at https://console.tencentcloud.com/cam/capi.region = None # "region" does not need to be specified if you initialize with "Endpoint".token = None # Token is required for temporary keys but not permanent keys. For more information about how to generate and use a temporary key, visit https://www.tencentcloud.com/document/product/436/14048.scheme = 'https' # Specify whether to use HTTP or HTTPS protocol to access COS. This field is optional and is `https` by default.endpoint = 'cos.accelerate.myqcloud.com' # Replace it with your endpoint or the COS global acceleration domain. To use a global acceleration domain, you need to enable the global acceleration feature for the bucket first (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)
bucket
nor region
will appear in the domain name.# -*- coding=utf-8from qcloud_cos import CosConfigfrom qcloud_cos import CosS3Clientimport sysimport logging# In most cases, set the log level to INFO. If you need to debug, you can set it to DEBUG and the SDK will print information about the communication with the server.logging.basicConfig(level=logging.INFO, stream=sys.stdout)# 1. Set user attributes such as secret_id, secret_key, and region. Appid has been removed from CosConfig and thus needs to be specified in Bucket, which is formatted as BucketName-Appid.secret_id = 'SecretId' # Replace it with the actual SecretId, which can be viewed and managed at https://console.tencentcloud.com/cam/capi.secret_key = 'SecretKey' # Replace it with the actual SecretKey, which can be viewed and managed at https://console.tencentcloud.com/cam/capi.region = None # "region" does not need to be specified if you initialize with a custom domain.token = None # Token is required for temporary keys but not permanent keys. For more information about how to generate and use a temporary key, visit https://www.tencentcloud.com/document/product/436/14048.scheme = 'https' # Specify whether to use HTTP or HTTPS protocol to access COS. This field is optional and is `https` by default.domain = 'user-define.example.com' # Your custom domain. You need to enable the custom domain for the bucket first (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)
# -*- coding=utf-8from qcloud_cos import CosConfigfrom qcloud_cos import CosS3Clientimport sysimport logging# In most cases, set the log level to INFO. If you need to debug, you can set it to DEBUG and the SDK will print the communication information of the client.logging.basicConfig(level=logging.INFO, stream=sys.stdout)# 1. Set user attributes such as secret_id, secret_key, and region. Appid has been removed from CosConfig and thus needs to be specified in Bucket, which is formatted as BucketName-Appid.secret_id = 'SecretId' # Replace it with the actual SecretId, which can be viewed and managed at https://console.tencentcloud.com/cam/capi.secret_key = 'SecretKey' # Replace it with the actual SecretKey, which can be viewed and managed at https://console.tencentcloud.com/cam/capi.region = None # "region" does not need to be specified if you initialize with "Endpoint".token = None # Token is required for temporary keys but not permanent keys. For more information about how to generate and use a temporary key, visit https://www.tencentcloud.com/document/product/436/14048.scheme = 'https' # Specify whether to use HTTP or HTTPS protocol to access COS. This field is optional and is `https` by default.endpoint = 'file.mycloud.com' # Replace it with your default CDN acceleration domain name. For details about how to enable 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)
bucket
nor region
will appear in the domain name) and then access COS via CDN origin server.# -*- coding=utf-8from qcloud_cos import CosConfigfrom qcloud_cos import CosS3Clientimport sysimport logging# In most cases, set the log level to INFO. If you need to debug, you can set it to DEBUG and the SDK will print information about the communication with the server.logging.basicConfig(level=logging.INFO, stream=sys.stdout)# 1. Set user attributes such as secret_id, secret_key, and region. Appid has been removed from CosConfig and thus needs to be specified in Bucket, which is formatted as BucketName-Appid.secret_id = 'SecretId' # Replace it with the actual SecretId, which can be viewed and managed at https://console.tencentcloud.com/cam/capi.secret_key = 'SecretKey' # Replace it with the actual SecretKey, which can be viewed and managed at https://console.tencentcloud.com/cam/capi.region = None # "region" does not need to be specified if you initialize with a custom domain.token = None # Token is required for temporary keys but not permanent keys. For more information about how to generate and use a temporary key, visit https://www.tencentcloud.com/document/product/436/14048.scheme = 'https' # Specify whether to use HTTP or HTTPS protocol to access COS. This field is optional and is `https` by default.domain = 'user-define.example-cdn.com' # Your CDN custom domain. You need to enable the CDN custom domain acceleration first (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, Domain=domain, Scheme=scheme)client = CosS3Client(config)
# -*- coding=utf-8from qcloud_cos import CosConfigfrom qcloud_cos import CosS3Clientimport sysimport logging# In most cases, set the log level to INFO. If you need to debug, you can set it to DEBUG and the SDK will print information about the communication with the server.logging.basicConfig(level=logging.INFO, stream=sys.stdout)# 1. Set user attributes such as secret_id, secret_key, and region. Appid has been removed from CosConfig and thus needs to be specified in Bucket, which is formatted as BucketName-Appid.secret_id = 'SecretId' # Replace it with the actual SecretId, which can be viewed and managed at https://console.tencentcloud.com/cam/capi.secret_key = 'SecretKey' # Replace it with the actual SecretKey, which can be viewed and managed at https://console.tencentcloud.com/cam/capi.region = 'ap-beijing' # Replace it with the actual region, which can be viewed in the console at https://console.tencentcloud.com/cos5/bucket.# For the list of regions supported by COS, see https://www.tencentcloud.com/document/product/436/6224?from_cn_redirect=1.token = None # Token is required for temporary keys but not permanent keys. For more information about how to generate and use a temporary key, visit https://www.tencentcloud.com/document/product/436/14048.scheme = 'https' # Specify whether to use HTTP or HTTPS protocol to access COS. This field is optional and is `https` by default.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-8from qcloud_cos import CosConfigfrom qcloud_cos import CosS3Clientimport sysimport logging# In most cases, set the log level to INFO. If you need to debug, you can set it to DEBUG and the SDK will print information about the communication with the server.logging.basicConfig(level=logging.INFO, stream=sys.stdout)# 1. Set user attributes such as secret_id, secret_key, and region. Appid has been removed from CosConfig and thus needs to be specified in Bucket, which is formatted as BucketName-Appid.secret_id = 'SecretId' # Replace it with the actual SecretId, which can be viewed and managed at https://console.tencentcloud.com/cam/capi.secret_key = 'SecretKey' # Replace it with the actual SecretKey, which can be viewed and managed at https://console.tencentcloud.com/cam/capi.region = 'ap-beijing' # Replace it with the actual region, which can be viewed in the console at https://console.tencentcloud.com/cos5/bucket.# For the list of regions supported by COS, see https://www.tencentcloud.com/document/product/436/6224?from_cn_redirect=1.token = None # Token is required for temporary keys but not permanent keys. For more information about how to generate and use a temporary key, visit https://www.tencentcloud.com/document/product/436/14048.scheme = 'https' # Specify whether to use HTTP or HTTPS protocol to access COS. This field is optional and is `https` by default.config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token, Scheme=scheme)client = CosS3Client(config)response = client.list_buckets()
# -*- coding=utf-8from qcloud_cos import CosConfigfrom qcloud_cos import CosS3Clientimport sysimport logging# In most cases, set the log level to INFO. If you need to debug, you can set it to DEBUG and the SDK will print information about the communication with the server.logging.basicConfig(level=logging.INFO, stream=sys.stdout)# 1. Set user attributes such as secret_id, secret_key, and region. Appid has been removed from CosConfig and thus needs to be specified in Bucket, which is formatted as BucketName-Appid.secret_id = 'SecretId' # Replace it with the actual SecretId, which can be viewed and managed at https://console.tencentcloud.com/cam/capi.secret_key = 'SecretKey' # Replace it with the actual SecretKey, which can be viewed and managed at https://console.tencentcloud.com/cam/capi.region = 'ap-beijing' # Replace it with the actual region, which can be viewed in the console at https://console.tencentcloud.com/cos5/bucket.# For the list of regions supported by COS, see https://www.tencentcloud.com/document/product/436/6224?from_cn_redirect=1.token = None # Token is required for temporary keys but not permanent keys. For more information about how to generate and use a temporary key, visit https://www.tencentcloud.com/document/product/436/14048.scheme = 'https' # Specify whether to use HTTP or HTTPS protocol to access COS. This field is optional and is `https` by default.config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token, Scheme=scheme)client = CosS3Client(config)#### Upload a file stream with simple upload (which cannot be used to upload files larger than 5 GB. For files larger than 5 GB, please use the advanced upload API described below).# It is strongly recommended to open the file in binary mode; otherwise, an error may occurwith 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'])#### Upload a byte stream with simple uploadresponse = client.put_object(Bucket='examplebucket-1250000000',Body=b'bytes',Key='picture.jpg',EnableMD5=False)print(response['ETag'])#### Simple chunk uploadimport requestsstream = requests.get('https://www.tencentcloud.com/document/product/436/7778?from_cn_redirect=1')# Online streams will be transferred to COS in the Transfer-Encoding:chunked manner.response = client.put_object(Bucket='examplebucket-1250000000',Body=stream,Key='picture.jpg')print(response['ETag'])#### Advanced upload API (recommended)# This API can automatically select between simple upload and multipart upload based on the file size. Multipart upload supports the checkpoint restart feature.response = client.upload_file(Bucket='examplebucket-1250000000',LocalFilePath='local.txt',Key='picture.jpg',PartSize=1,MAXThread=10,EnableMD5=False)print(response['ETag'])
# -*- coding=utf-8from qcloud_cos import CosConfigfrom qcloud_cos import CosS3Clientimport sysimport logging# In most cases, set the log level to INFO. If you need to debug, you can set it to DEBUG and the SDK will print information about the communication with the server.logging.basicConfig(level=logging.INFO, stream=sys.stdout)# 1. Set user attributes such as secret_id, secret_key, and region. Appid has been removed from CosConfig and thus needs to be specified in Bucket, which is formatted as BucketName-Appid.secret_id = 'SecretId' # Replace it with the actual SecretId, which can be viewed and managed at https://console.tencentcloud.com/cam/capi.secret_key = 'SecretKey' # Replace it with the actual SecretKey, which can be viewed and managed at https://console.tencentcloud.com/cam/capi.region = 'ap-beijing' # Replace it with the actual region, which can be viewed in the console at https://console.tencentcloud.com/cos5/bucket.# For the list of regions supported by COS, see https://www.tencentcloud.com/document/product/436/6224?from_cn_redirect=1.token = None # Token is required for temporary keys but not permanent keys. For more information about how to generate and use a temporary key, visit https://www.tencentcloud.com/document/product/436/14048.scheme = 'https' # Specify whether to use HTTP or HTTPS protocol to access COS. This field is optional and is `https` by default.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')
list_objects
API can query up to 1,000 objects. If you want to query all objects, you need to call it repeatedly.# -*- coding=utf-8from qcloud_cos import CosConfigfrom qcloud_cos import CosS3Clientimport sysimport logging# In most cases, set the log level to INFO. If you need to debug, you can set it to DEBUG and the SDK will print information about the communication with the server.logging.basicConfig(level=logging.INFO, stream=sys.stdout)# 1. Set user attributes such as secret_id, secret_key, and region. Appid has been removed from CosConfig and thus needs to be specified in Bucket, which is formatted as BucketName-Appid.secret_id = 'SecretId' # Replace it with the actual SecretId, which can be viewed and managed at https://console.tencentcloud.com/cam/capi.secret_key = 'SecretKey' # Replace it with the actual SecretKey, which can be viewed and managed at https://console.tencentcloud.com/cam/capi.region = 'ap-beijing' # Replace it with the actual region, which can be viewed in the console at https://console.tencentcloud.com/cos5/bucket.# For the list of regions supported by COS, see https://www.tencentcloud.com/document/product/436/6224?from_cn_redirect=1.token = None # Token is required for temporary keys but not permanent keys. For more information about how to generate and use a temporary key, visit https://www.tencentcloud.com/document/product/436/14048.scheme = 'https' # Specify whether to use HTTP or HTTPS protocol to access COS. This field is optional and is `https` by default.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)print(response['Contents'])if response['IsTruncated'] == 'false':breakmarker = response['NextMarker']
# -*- coding=utf-8from qcloud_cos import CosConfigfrom qcloud_cos import CosS3Clientimport sysimport logging# In most cases, set the log level to INFO. If you need to debug, you can set it to DEBUG and the SDK will print information about the communication with the server.logging.basicConfig(level=logging.INFO, stream=sys.stdout)# 1. Set user attributes such as secret_id, secret_key, and region. Appid has been removed from CosConfig and thus needs to be specified in Bucket, which is formatted as BucketName-Appid.secret_id = 'SecretId' # Replace it with the actual SecretId, which can be viewed and managed at https://console.tencentcloud.com/cam/capi.secret_key = 'SecretKey' # Replace it with the actual SecretKey, which can be viewed and managed at https://console.tencentcloud.com/cam/capi.region = 'ap-beijing' # Replace it with the actual region, which can be viewed in the console at https://console.tencentcloud.com/cos5/bucket.# For the list of regions supported by COS, see https://www.tencentcloud.com/document/product/436/6224?from_cn_redirect=1.token = None # Token is required for temporary keys but not permanent keys. For more information about how to generate and use a temporary key, visit https://www.tencentcloud.com/document/product/436/14048.scheme = 'https' # Specify whether to use HTTP or HTTPS protocol to access COS. This field is optional and is `https` by default.config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token, Scheme=scheme)client = CosS3Client(config)#### Download an object to the local file systemresponse = client.get_object(Bucket='examplebucket-1250000000',Key='picture.jpg',)response['Body'].get_stream_to_file('output.txt')#### Download a file streamresponse = client.get_object(Bucket='examplebucket-1250000000',Key='picture.jpg',)fp = response['Body'].get_raw_stream()print(fp.read(2))#### Set the response HTTP headersresponse = 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 rangeresponse = client.get_object(Bucket='examplebucket-1250000000',Key='picture.jpg',Range='bytes=0-10')fp = response['Body'].get_raw_stream()print(fp.read())
# -*- coding=utf-8from qcloud_cos import CosConfigfrom qcloud_cos import CosS3Clientimport sysimport logging# In most cases, set the log level to INFO. If you need to debug, you can set it to DEBUG and the SDK will print information about the communication with the server.logging.basicConfig(level=logging.INFO, stream=sys.stdout)# 1. Set user attributes such as secret_id, secret_key, and region. Appid has been removed from CosConfig and thus needs to be specified in Bucket, which is formatted as BucketName-Appid.secret_id = 'SecretId' # Replace it with the actual SecretId, which can be viewed and managed at https://console.tencentcloud.com/cam/capi.secret_key = 'SecretKey' # Replace it with the actual SecretKey, which can be viewed and managed at https://console.tencentcloud.com/cam/capi.region = 'ap-beijing' # Replace it with the actual region, which can be viewed in the console at https://console.tencentcloud.com/cos5/bucket.# For the list of regions supported by COS, see https://www.tencentcloud.com/document/product/436/6224?from_cn_redirect=1.token = None # Token is required for temporary keys but not permanent keys. For more information about how to generate and use a temporary key, visit https://www.tencentcloud.com/document/product/436/14048.scheme = 'https' # Specify whether to use HTTP or HTTPS protocol to access COS. This field is optional and is `https` by default.config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token, Scheme=scheme)client = CosS3Client(config)# Delete an object## deleteObjectresponse = client.delete_object(Bucket='examplebucket-1250000000',Key='exampleobject')# Delete multiple objects## deleteObjectsresponse = client.delete_objects(Bucket='examplebucket-1250000000',Delete={'Object': [{'Key': 'exampleobject1',},{'Key': 'exampleobject2',},],'Quiet': 'true'|'false'})
Was this page helpful?