pip install -U cos-python-sdk-v5
python setup.py install
# Run the following command on a machine with external network.mkdir 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 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.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 osimport 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=1token = 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)
# -*- coding=utf-8from qcloud_cos import CosConfigfrom qcloud_cos import CosS3Clientimport sysimport osimport 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=1scheme = '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)
# -*- coding=utf-8from qcloud_cos import CosConfigfrom qcloud_cos import CosS3Clientimport sysimport osimport 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=1token = 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)
# -*- coding=utf-8from qcloud_cos import CosConfigfrom qcloud_cos import CosS3Clientimport sysimport osimport 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 regiontoken = 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)
# -*- coding=utf-8from qcloud_cos import CosConfigfrom qcloud_cos import CosS3Clientimport sysimport osimport 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 regiontoken = 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)
# -*- coding=utf-8from qcloud_cos import CosConfigfrom qcloud_cos import CosS3Clientimport sysimport osimport 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 regiontoken = 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=1config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token, Endpoint=endpoint, Scheme=scheme)client = CosS3Client(config)
# -*- coding=utf-8from qcloud_cos import CosConfigfrom qcloud_cos import CosS3Clientimport sysimport osimport 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 regiontoken = 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=1config = 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 osimport 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=1token = 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-8from qcloud_cos import CosConfigfrom qcloud_cos import CosS3Clientimport sysimport osimport 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=1token = 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()
# -*- coding=utf-8from qcloud_cos import CosConfigfrom qcloud_cos import CosS3Clientimport sysimport osimport 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=1token = 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 streamsresponse = client.put_object(Bucket='examplebucket-1250000000',Body=b'bytes',Key='picture.jpg',EnableMD5=False)print(response['ETag'])#### Simple upload of chunksimport requestsstream = 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-8from qcloud_cos import CosConfigfrom qcloud_cos import CosS3Clientimport sysimport osimport 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=1token = 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')
list_objects API can only retrieve 1000 objects. If you need to query all objects, you must make repeated calls.# -*- coding=utf-8from qcloud_cos import CosConfigfrom qcloud_cos import CosS3Clientimport sysimport osimport 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=1token = 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':breakmarker = response['NextMarker']
# -*- coding=utf-8from qcloud_cos import CosConfigfrom qcloud_cos import CosS3Clientimport sysimport osimport 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=1token = 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())
# -*- coding=utf-8from qcloud_cos import CosConfigfrom qcloud_cos import CosS3Clientimport sysimport osimport 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=1token = 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.## 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' # Specifies the method for the response to deletion. Valid values: true, false})
Feedback