Uploading Objects

Last updated: 2024-11-25 15:48:48

Feature Overview

This document provides an overview of APIs and SDK code samples for advanced upload, upload in whole, multipart upload, and other object operations.
Simple operations
API
Operation
Description
Uploading an object using simple upload
Uploads an object to a bucket
Appending parts
Appends object parts to a bucket.
Multipart operations
API
Operation
Description
Querying multipart upload
Queries the information on ongoing multipart uploads
Initializing a multipart upload operation
Initializes a multipart upload task
Uploading parts
Uploads a file in multiple parts
Copying a part
Copies an object as a part
Querying uploaded parts
Queries uploaded parts in a specified multipart upload operation
Completing multipart upload
Completes the multipart upload of the entire file
Aborting a multipart upload
Aborts a multipart upload operation and deletes the uploaded parts

Advanced APIs (Recommended)

Uploading an object (checkpoint restart)

Note

The advanced interface automatically selects between simple upload and multipart upload based on the file size. Files less than or equal to 20MB will use simple upload, while files larger than 20MB will use multipart upload. Incomplete multipart uploads will automatically resume from the breakpoint. For multipart uploads, you can use the progress_callback function to track the upload progress.

Method prototype

upload_file(Bucket, Key, LocalFilePath, PartSize=1, MAXThread=5, EnableMD5=False, progress_callback=None, **kwargs)

Sample Request

# -*- coding=utf-8
from qcloud_cos import CosConfig
from qcloud_cos import CosS3Client
from qcloud_cos.cos_exception import CosClientError, CosServiceError
import sys
import os
import logging

# Under normal circumstances, use the INFO log level. To locate issues, change it to DEBUG, and 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, etc. 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 <1>SecretId</1>. We recommend that you use a sub-account key and follow the principle of least privilege to reduce risks. For more information on how to obtain a sub-account key, visit https://cloud.tencent.com/document/product/598/37140.
secret_key = os.environ['COS_SECRET_KEY'] # User <1>SecretKey</1>. We recommend that you use a sub-account key and follow the principle of least privilege to reduce risks. For more information on how to obtain a sub-account key, visit https://cloud.tencent.com/document/product/598/37140.
region = 'ap-beijing' # Replace it with the actual region, which can be viewed in the console at https://console.cloud.tencent.com/cos5/bucket.
For a list of all regions supported by COS, visit https://cloud.tencent.com/document/product/436/6224
token = None # Token is required for temporary keys but not permanent keys. For more information about how to generate and use a temporary key, see https://cloud.tencent.com/document/product/436/14048.
scheme = 'https' # Specify whether to use HTTP or HTTPS protocol to access COS. This 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 once using the advanced API without retrying, thus not utilizing the checkpoint restart feature.
response = client.upload_file(
Bucket='examplebucket-1250000000',
Key='exampleobject',
LocalFilePath='local.txt',
EnableMD5=False,
progress_callback=None
)

# Use the advanced interface for checkpoint restart; failed retries will not re-upload successfully completed parts (retry 10 times here)
for i in range(0, 10):
try:
response = client.upload_file(
Bucket='examplebucket-1250000000',
Key='exampleobject',
LocalFilePath='local.txt')
break
except CosClientError or CosServiceError as e:
print(e)

Sample request with all parameters

def upload_percentage(consumed_bytes, total_bytes):
"""Progress bar callback function, calculating the current upload percentage

:param consumed_bytes: Amount of data already uploaded
:param total_bytes: Total data volume
"""
if total_bytes:
rate = int(100 * (float(consumed_bytes) / float(total_bytes)))
print('\r{0}% '.format(rate))
sys.stdout.flush()

response = client.upload_file(
Bucket='examplebucket-1250000000',
Key='exampleobject',
LocalFilePath='local.txt',
PartSize=1,
MAXThread=5,
progress_callback=upload_percentage,
EnableMD5=False|True,
ACL='private'|'public-read', # Use this parameter with caution, as it may reach the 1000 ACL limit.
GrantFullControl='string',
GrantRead='string',
StorageClass='STANDARD'|'STANDARD_IA'|'ARCHIVE',
Expires='string',
CacheControl='string',
ContentType='string',
ContentDisposition='string',
ContentEncoding='string',
ContentLanguage='string',
ContentLength='123',
ContentMD5='string',
Metadata={
'x-cos-meta-key1': 'value1',
'x-cos-meta-key2': 'value2'
},
TrafficLimit='1048576'
)

Description

Parameter name
ParameterDescription
Local Disk Types
Required
Bucket
Bucket name in the format of BucketName-APPID
String
Required
Key
ObjectKey is the unique identifier of the object in the bucket. For example, in the object's access domain name examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/doc/pic.jpg, the ObjectKey is doc/pic.jpg
String
Required
LocalFilePath
Path to the local file
String
Required
PartSize
Part size; the default value is 1 MB
Int
Not required
MAXThread
Maximum number of threads for concurrent multipart uploads; the default value is 5
Int
Not required
progress_callback
Callback function for the upload progress. You can customize this function to query the upload progress.
Func
Not required
EnableMD5
Specifies whether the SDK needs to calculate the Content-MD5 value. This feature is disabled by default. The upload will take longer if it is enabled
Bool
Not required
ACL
Sets the object ACL, such as private or public-read
String
Not required
GrantFullControl
Grant all permissions to the authorized party, with the format id="OwnerUin", for example, id="100000000001".
String
Not required
GrantRead
Grant read permission to the authorized user, with the format id="OwnerUin", for example id="100000000001".
String
Not required
StorageClass
Set the storage class of an object, such as STANDARD, STANDARD_IA, or ARCHIVE. The default value is STANDARD. For more storage classes, see Storage Class Overview.
String
Not required
Expires
Sets Expires.
String
Not required
CacheControl
Cache policy. Sets Cache-Control.
String
Not required
ContentType
Content type. Sets Content-Type
String
Not required
ContentDisposition
Filename
String
Not required
ContentEncoding
Encoding format. Sets Content-Encoding
String
Not required
ContentLanguage
Language type. Sets Content-Language
String
Not required
ContentLength
Length of the content
String
Not required
ContentMD5
Sets the MD5 checksum of the uploaded object
String
Not required
Metadata
User-defined object metadata
Dict
Not required
TrafficLimit
The rate limit value for a single connection is in bits per second (bit/s), with a range of 819200 to 838860800, or 800Kb/s to 800Mb/s. The advanced interface restricts the speed of a single thread.
String
Not required

Response description

The response contains the attributes of the uploaded object in dict format:
{
'ETag': 'string'
'x-cos-hash-crc64ecma': 'string',
'x-cos-request-id': 'string',
'x-cos-storage-class': 'string',
'Location': 'string', # This field is returned when initiating a multipart upload
'Bucket': 'string', # This field is returned when initiating a multipart upload
'Key': 'string', # This field is returned when initiating a multipart upload
}
Parameter name
ParameterDescription
Local Disk Types
ETag
For multipart uploads, this value does not represent the MD5 checksum of the object content, but is used only to verify the uniqueness of the object.
String
x-cos-hash-crc64ecma
CRC64 checksum of the file
String
x-cos-request-id
Request ID
String
x-cos-storage-class
Types of Storage
String
Location
Access URL of the object (returned when initiating a multipart upload)
String
Bucket
Bucket name (returned when initiating a multipart upload)
String
Key
Object Name (returned when initiating a multipart upload)
String

Batch upload (uploading a local folder)

Note

The following sample uses the basic SDK APIs to upload a local folder to COS:

Sample Request

# -*- coding=utf-8
from qcloud_cos import CosConfig
from qcloud_cos import CosS3Client
from qcloud_cos import CosServiceError
from qcloud_cos import CosClientError
from qcloud_cos.cos_threadpool import SimpleThreadPool

import sys
import os
import logging

# Under normal circumstances, use the INFO log level. To locate issues, change it to DEBUG, and the SDK will print communication information with the server.
logging.basicConfig(level=logging.INFO, stream=sys.stdout)

# Set user attributes, including secret_id, secret_key, region, etc. 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 <1>SecretId</1>. We recommend that you use a sub-account key and follow the principle of least privilege to reduce risks. For more information on how to obtain a sub-account key, visit https://cloud.tencent.com/document/product/598/37140.
secret_key = os.environ['COS_SECRET_KEY'] # User <1>SecretKey</1>. We recommend that you use a sub-account key and follow the principle of least privilege to reduce risks. For more information on how to obtain a sub-account key, visit https://cloud.tencent.com/document/product/598/37140.
region = 'ap-beijing' # Replace it with the actual region, which can be viewed in the console at https://console.cloud.tencent.com/cos5/bucket.
For a list of all regions supported by COS, visit https://cloud.tencent.com/document/product/436/6224
token = None # Token is required for temporary keys but not permanent keys. For more information about how to generate and use a temporary key, see https://cloud.tencent.com/document/product/436/14048.

config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token) # Get the configured object
client = CosS3Client(config)

uploadDir = '/root/logs'
bucket = 'examplebucket-125000000'
g = os.walk(uploadDir)
# Create an upload thread pool
pool = SimpleThreadPool()
for path, dir_list, file_list in g:
for file_name in file_list:
srcKey = os.path.join(path, file_name)
cosObjectKey = srcKey.strip('/')
# Determine if a file exists in COS
exists = False
try:
response = client.head_object(Bucket=bucket, Key=cosObjectKey)
exists = True
except CosServiceError as e:
if e.get_status_code() == 404:
exists = False
else:
print("Error happened, reupload it.")
if not exists:
print("File %s not exists in cos, upload it", srcKey)
pool.add_task(client.upload_file, bucket, cosObjectKey, srcKey)


pool.wait_completion()
result = pool.get_result()
if not result['success_all']:
print("Not all files upload successed. you should retry")

Simple Operations

Uploading an object using simple upload

Note

This API (PUT Object) is used to upload an object to a bucket.

Method prototype

put_object(Bucket, Body, Key, **kwargs)

Sample 1. Simple object upload

# -*- 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. To locate issues, change it to DEBUG, and 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, etc. 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 <1>SecretId</1>. We recommend that you use a sub-account key and follow the principle of least privilege to reduce risks. For more information on how to obtain a sub-account key, visit https://cloud.tencent.com/document/product/598/37140.
secret_key = os.environ['COS_SECRET_KEY'] # User <1>SecretKey</1>. We recommend that you use a sub-account key and follow the principle of least privilege to reduce risks. For more information on how to obtain a sub-account key, visit https://cloud.tencent.com/document/product/598/37140.
region = 'ap-beijing' # Replace it with the actual region, which can be viewed in the console at https://console.cloud.tencent.com/cos5/bucket.
For a list of all regions supported by COS, visit https://cloud.tencent.com/document/product/436/6224
token = None # Token is required for temporary keys but not permanent keys. For more information about how to generate and use a temporary key, see https://cloud.tencent.com/document/product/436/14048.
scheme = 'https' # Specify whether to use HTTP or HTTPS protocol to access COS. This is optional and is https by default.

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

# Simple upload using file stream
file_name = 'test.txt'
with open('test.txt', 'rb') as fp:
response = client.put_object(
Bucket='examplebucket-1250000000', # Bucket is composed of BucketName-APPID
Body=fp,
Key=file_name,
StorageClass='STANDARD',
ContentType='text/html; charset=utf-8'
)
print(response['ETag'])

# Byte stream simple upload
response = client.put_object(
Bucket='examplebucket-1250000000',
Body=b'abcdefg',
Key=file_name
)
print(response['ETag'])

# Simple upload from local path
response = client.put_object_from_local_file(
Bucket='examplebucket-1250000000',
LocalFilePath='local.txt',
Key=file_name,
)
print(response['ETag'])

# Set HTTP header for simple upload
response = client.put_object(
Bucket='examplebucket-1250000000',
Body=b'test',
Key=file_name,
ContentType='text/html; charset=utf-8'
)
print(response['ETag'])

# Set custom header - Simple upload
response = client.put_object(
Bucket='examplebucket-1250000000',
Body=b'test',
Key=file_name,
Metadata={
'x-cos-meta-key1': 'value1',
'x-cos-meta-key2': 'value2'
}
)
print(response['ETag'])

# Upload Speed Limit
with open('test.bin', 'rb') as fp:
response = client.put_object(
Bucket='examplebucket-1250000000',
Key='exampleobject',
Body=fp,
TrafficLimit='819200'
)
print(response['ETag'])

Sample 2: creating a directory

In COS, a directory is an object whose name ends with a slash (/). Therefore, you can call the Put Object API.
# -*- 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. To locate issues, change it to DEBUG, and 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, etc. 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 <1>SecretId</1>. We recommend that you use a sub-account key and follow the principle of least privilege to reduce risks. For more information on how to obtain a sub-account key, visit https://cloud.tencent.com/document/product/598/37140.
secret_key = os.environ['COS_SECRET_KEY'] # User <1>SecretKey</1>. We recommend that you use a sub-account key and follow the principle of least privilege to reduce risks. For more information on how to obtain a sub-account key, visit https://cloud.tencent.com/document/product/598/37140.
region = 'ap-beijing' # Replace it with the actual region, which can be viewed in the console at https://console.cloud.tencent.com/cos5/bucket.
For a list of all regions supported by COS, visit https://cloud.tencent.com/document/product/436/6224
token = None # Token is required for temporary keys but not permanent keys. For more information about how to generate and use a temporary key, see https://cloud.tencent.com/document/product/436/14048.
scheme = 'https' # Specify whether to use HTTP or HTTPS protocol to access COS. This is optional and is https by default.

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

# Creating directory
dir_to_create='path/to/create/dir/'
response = client.put_object(
Bucket='examplebucket-1250000000', # Bucket is composed of BucketName-APPID
Key=dir_to_create,
Body=b'',
)
print(response)

Sample 3: uploading an object to a specified directory

# -*- 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. To locate issues, change it to DEBUG, and 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, etc. 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 <1>SecretId</1>. We recommend that you use a sub-account key and follow the principle of least privilege to reduce risks. For more information on how to obtain a sub-account key, visit https://cloud.tencent.com/document/product/598/37140.
secret_key = os.environ['COS_SECRET_KEY'] # User <1>SecretKey</1>. We recommend that you use a sub-account key and follow the principle of least privilege to reduce risks. For more information on how to obtain a sub-account key, visit https://cloud.tencent.com/document/product/598/37140.
region = 'ap-beijing' # Replace it with the actual region, which can be viewed in the console at https://console.cloud.tencent.com/cos5/bucket.
For a list of all regions supported by COS, visit https://cloud.tencent.com/document/product/436/6224
token = None # Token is required for temporary keys but not permanent keys. For more information about how to generate and use a temporary key, see https://cloud.tencent.com/document/product/436/14048.
scheme = 'https' # Specify whether to use HTTP or HTTPS protocol to access COS. This is optional and is https by default.

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

You can upload an object whose name is separated with slashes. In this way, the directory that contains this object will be created automatically. If you need to upload new objects to this COS directory, you can pass the value of this directory to <1>dir</1>.
dir_name = 'path/to/dir/'
file_name = 'test.txt'
object_key = dir_name + file_name
with open('test.txt', 'rb') as fp:
response = client.put_object(
Bucket='examplebucket-1250000000', # Bucket is composed of BucketName-APPID
Body=fp,
Key=object_key,
StorageClass='STANDARD',
ContentType='text/html; charset=utf-8'
)
print(response['ETag'])

Sample request with all parameters

response = client.put_object(
Bucket='examplebucket-1250000000',
Body=b'bytes'|file,
Key='exampleobject',
EnableMD5=False|True,
ACL='private'|'public-read', # Use this parameter with caution, as it may reach the 1000 ACL limit.
GrantFullControl='string',
GrantRead='string',
StorageClass='STANDARD'|'STANDARD_IA'|'ARCHIVE',
Expires='string',
CacheControl='string',
ContentType='string',
ContentDisposition='string',
ContentEncoding='string',
ContentLanguage='string',
ContentLength='123',
ContentMD5='string',
Metadata={
'x-cos-meta-key1': 'value1',
'x-cos-meta-key2': 'value2'
},
TrafficLimit='1048576'
)

Description

Parameter name
ParameterDescription
Local Disk Types
Required
Bucket
Bucket name in the format of BucketName-APPID
String
Required
Body
Content of the uploaded object, which can be file stream or byte stream
file/bytes
Required
Key
ObjectKey is the unique identifier of the object in the bucket. For example, in the object's access domain name examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/doc/pic.jpg, the ObjectKey is doc/pic.jpg
String
Required
EnableMD5
Specifies whether the SDK needs to calculate the Content-MD5 value. This feature is disabled by default. The upload will take longer if it is enabled
Bool
Not required
ACL
Sets the object ACL, e.g. 'private', 'public-read'
String
Not required
GrantFullControl
Grant all permissions to the authorized party, with the format id="OwnerUin", for example, id="100000000001".
String
Not required
GrantRead
Grant read permission to the authorized user, with the format id="OwnerUin", for example id="100000000001".
String
Not required
StorageClass
Set the storage class of an object to STANDARD, STANDARD_IA, or ARCHIVE. The default value is STANDARD. For more storage classes, see Storage Class Overview.
String
Not required
Expires
Sets Expires.
String
Not required
CacheControl
Cache policy. Sets Cache-Control.
String
Not required
ContentType
Content type. Sets Content-Type
String
Not required
ContentDisposition
Object name
String
Not required
ContentEncoding
Encoding format. Sets Content-Encoding
String
Not required
ContentLanguage
Language type. Sets Content-Language
String
Not required
ContentLength
Length of the content
String
Not required
ContentMD5
Sets the MD5 checksum of the uploaded object
String
Not required
Metadata
User-defined object metadata. It must start with x-cos-meta; otherwise, it will be ignored
Dict
Not required
TrafficLimit
The single-link speed limit value is in bits per second (bit/s), with a range of 819200 to 838860800, which is equivalent to 800 Kb/s to 800 Mb/s.
String
Not required

Response description

The response contains the attributes of the uploaded object in dict format:
{
'ETag': 'string',
'x-cos-version-id': 'string'
}
Parameter name
ParameterDescription
Local Disk Types
ETag
MD5 checksum of the uploaded object
String
x-cos-version-id
Version ID of the object if versioning is enabled
String

Appending parts

Note

This API (APPEND Object) is used to append object parts to a bucket.

Method prototype

append_object(Bucket, Key, Position, Data, **kwargs)

Sample Request

# -*- 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. To locate issues, change it to DEBUG, and 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, etc. 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 <1>SecretId</1>. We recommend that you use a sub-account key and follow the principle of least privilege to reduce risks. For more information on how to obtain a sub-account key, visit https://cloud.tencent.com/document/product/598/37140.
secret_key = os.environ['COS_SECRET_KEY'] # User <1>SecretKey</1>. We recommend that you use a sub-account key and follow the principle of least privilege to reduce risks. For more information on how to obtain a sub-account key, visit https://cloud.tencent.com/document/product/598/37140.
region = 'ap-beijing' # Replace it with the actual region, which can be viewed in the console at https://console.cloud.tencent.com/cos5/bucket.
For a list of all regions supported by COS, visit https://cloud.tencent.com/document/product/436/6224
token = None # Token is required for temporary keys but not permanent keys. For more information about how to generate and use a temporary key, see https://cloud.tencent.com/document/product/436/14048.
scheme = 'https' # Specify whether to use HTTP or HTTPS protocol to access COS. This 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.append_object(
Bucket='examplebucket-1250000000',
Key='exampleobject',
Position=0,
Data=b'b'*1024*1024
)

Sample request with all parameters

response = client.append_object(
Bucket='examplebucket-1250000000',
Key='exampleobject',
Position=0,
Data=b'bytes'|file
)

Description

Parameter name
ParameterDescription
Local Disk Types
Required
Bucket
Bucket name in the format of BucketName-APPID
String
Required
Key
ObjectKey is the unique identifier of the object in the bucket. For example, in the object's access domain name examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/doc/pic.jpg, the ObjectKey is doc/pic.jpg
String
Required
Position
Starting point for the append operation (in bytes). For the first append, the value of this parameter is 0. For subsequent appends, the value is the content-length of the current object.
Int
Required
Data
Content of the uploaded part, which can be file stream or byte stream
file/bytes
Required

Response description

The response contains the attributes of the object in dict format after the append operation.
{
'ETag': '"9a4802d5c99dafe1c04da0a8e7e166bf"',
'x-cos-next-append-position': '12',
'x-cos-request-id': 'NjEwN2Q0ZGZfMWNhZjU4NjRfMzM1M19hNzQzYjc2'
}

Multipart Operations

For more information on multipart uploads, see Multipart Upload. The operations that can be included in a multipart upload are as follows:
Uploading objects with multipart upload: initializing a multipart upload, uploading parts, and completing a multipart upload.
Resuming a multipart upload: querying uploaded parts, uploading remaining parts, and completing a multipart upload.
Deleting uploaded parts.

Querying multipart upload

Note

This API is used to query in-progress multipart uploads in a specified bucket.

Method prototype

list_multipart_uploads(Bucket, Prefix="", Delimiter="", KeyMarker="", UploadIdMarker="", MaxUploads=1000, EncodingType="", **kwargs)

Sample Request

# -*- 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. To locate issues, change it to DEBUG, and 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, etc. 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 <1>SecretId</1>. We recommend that you use a sub-account key and follow the principle of least privilege to reduce risks. For more information on how to obtain a sub-account key, visit https://cloud.tencent.com/document/product/598/37140.
secret_key = os.environ['COS_SECRET_KEY'] # User <1>SecretKey</1>. We recommend that you use a sub-account key and follow the principle of least privilege to reduce risks. For more information on how to obtain a sub-account key, visit https://cloud.tencent.com/document/product/598/37140.
region = 'ap-beijing' # Replace it with the actual region, which can be viewed in the console at https://console.cloud.tencent.com/cos5/bucket.
For a list of all regions supported by COS, visit https://cloud.tencent.com/document/product/436/6224
token = None # Token is required for temporary keys but not permanent keys. For more information about how to generate and use a temporary key, see https://cloud.tencent.com/document/product/436/14048.
scheme = 'https' # Specify whether to use HTTP or HTTPS protocol to access COS. This 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_multipart_uploads(
Bucket='examplebucket-1250000000',
Prefix='dir'
)

Sample request with all parameters

response = client.list_multipart_uploads(
Bucket='examplebucket-1250000000',
Prefix='string',
Delimiter='string',
KeyMarker='string',
UploadIdMarker='string',
MaxUploads=100,
EncodingType='url'
)

Description

Parameter name
ParameterDescription
Local Disk Types
Required
Bucket
Bucket name in the format of BucketName-APPID
String
Required
Prefix
Object key prefix to filter multipart uploads by. It is left empty by default.
String
Not required
Delimiter
A separator that is left empty by default.
String
Not required
KeyMarker
The key of the object after which the returned list begins. It is used together with UploadIdMarker.
String
Not required
UploadIdMarker
The upload ID of the object after which the returned list begins. It is used together with KeyMarker. If KeyMarker is not specified, UploadIdMarker will be ignored.
String
Not required
MaxUploads
Maximum number of multipart uploads returned at a time. The default value is 1000.
Int
Not required
EncodingType
Indicates the encoding method of the returned value. The value is not encoded by default. Valid value: url
String
Not required

Response description

This response contains information on the multipart uploads in dict format:
{
'Bucket': 'examplebucket-1250000000',
'Prefix': 'string',
'Delimiter': 'string',
'KeyMarker': 'string',
'UploadIdMarker': 'string',
'NextKeyMarker': 'string',
'NextUploadIdMarker': 'string',
'MaxUploads': '1000',
'IsTruncated': 'true'|'false',,
'EncodingType': 'url',
'Upload':[
{
'UploadId': 'string',
'Key': 'string',
'Initiated': 'string',
'StorageClass': 'STANDARD',
'Owner': {
'DisplayName': 'string',
'ID': 'string'
},
'Initiator': {
'ID': 'string',
'DisplayName': 'string'
}
},
],
'CommonPrefixes':[
{
'Prefix': 'string'
},
],
}
Parameter name
ParameterDescription
Local Disk Types
Bucket
Bucket name in the format of BucketName-APPID
String
Prefix
Object key prefix to filter multipart uploads by. It is left empty by default.
String
Delimiter
A separator that is left empty by default.
String
KeyMarker
The key of the object after which the returned list begins. It is used together with UploadIdMarker.
String
UploadIdMarker
The upload ID of the object after which the returned list begins. If KeyMarker is not specified, UploadIdMarker will be ignored.
String
NextKeyMarker
The key of the object after which the next returned list begins if IsTruncated is true
String
NextUploadIdMarker
The upload ID of the object after which the next returned list begins if IsTruncated is true
String
MaxUploads
Maximum number of multipart uploads returned at a time. The default value is 1000.
Int
IsTruncated
Whether the returned multipart upload list is truncated
String
EncodingType
Indicates the encoding method of the returned value. The value is not encoded by default. Valid value: url
String
Upload
List of information on all the returned multipart uploads, including UploadId, StorageClass, Key, Owner, Initiator, and Initiated
List
CommonPrefixes
Groups all keys starting with Prefix and ending with Delimiter as a common prefix.
List

Initializing a multipart upload operation

Note

This API (Initiate Multipart Upload) is used to initialize a multipart upload and obtain its uploadId.

Method prototype

create_multipart_upload(Bucket, Key, **kwargs):

Sample Request

# -*- 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. To locate issues, change it to DEBUG, and 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, etc. 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 <1>SecretId</1>. We recommend that you use a sub-account key and follow the principle of least privilege to reduce risks. For more information on how to obtain a sub-account key, visit https://cloud.tencent.com/document/product/598/37140.
secret_key = os.environ['COS_SECRET_KEY'] # User <1>SecretKey</1>. We recommend that you use a sub-account key and follow the principle of least privilege to reduce risks. For more information on how to obtain a sub-account key, visit https://cloud.tencent.com/document/product/598/37140.
region = 'ap-beijing' # Replace it with the actual region, which can be viewed in the console at https://console.cloud.tencent.com/cos5/bucket.
For a list of all regions supported by COS, visit https://cloud.tencent.com/document/product/436/6224
token = None # Token is required for temporary keys but not permanent keys. For more information about how to generate and use a temporary key, see https://cloud.tencent.com/document/product/436/14048.
scheme = 'https' # Specify whether to use HTTP or HTTPS protocol to access COS. This 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_multipart_upload(
Bucket='examplebucket-1250000000',
Key='exampleobject',
StorageClass='STANDARD'
)

Sample request with all parameters

response = client.create_multipart_upload(
Bucket='examplebucket-1250000000',
Key='multipart.txt',
StorageClass='STANDARD'|'STANDARD_IA'|'ARCHIVE',
Expires='string',
CacheControl='string',
ContentType='string',
ContentDisposition='string',
ContentEncoding='string',
ContentLanguage='string',
Metadata={
'x-cos-meta-key1': 'value1',
'x-cos-meta-key2': 'value2'
},
ACL='private'|'public-read',
GrantFullControl='string',
GrantRead='string'
)
# Obtain UploadId for subsequent interface usage
uploadid = response['UploadId']

Description

Parameter name
ParameterDescription
Local Disk Types
Required
Bucket
Bucket name in the format of BucketName-APPID
String
Required
Key
ObjectKey is the unique identifier of the object in the bucket. For example, in the object's access domain name examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/doc/pic.jpg, the ObjectKey is doc/pic.jpg
String
Required
StorageClass
Set the storage class of an object to STANDARD, STANDARD_IA, or ARCHIVE. The default value is STANDARD. For more storage classes, see Storage Class Overview.
String
Not required
Expires
Sets Expires.
String
Not required
CacheControl
Cache policy. Sets Cache-Control.
String
Not required
ContentType
Content type. Sets Content-Type
String
Not required
ContentDisposition
Filename
String
Not required
ContentEncoding
Encoding format. Sets Content-Encoding
String
Not required
ContentLanguage
Language type. Sets Content-Language
String
Not required
Metadata
User-defined object metadata
Dict
Not required
ACL
Sets the object ACL, e.g. 'private', 'public-read'
String
Not required
GrantFullControl
Grant all permissions to the authorized party, with the format id="OwnerUin", for example, id="100000000001".
String
Not required
GrantRead
Grant read permission to the authorized user, with the format id="OwnerUin", for example id="100000000001".
String
Not required

Response description

This response contains the initialization information of the multipart upload in DICT format:
{
'UploadId': '150219101333cecfd6718d0caea1e2738401f93aa531a4be7a2afee0f8828416f3278e5570',
'Bucket': 'examplebucket-1250000000',
'Key': 'exampleobject'
}

Parameter name
ParameterDescription
Local Disk Types
UploadId
ID that identifies the multipart upload
String
Bucket
Bucket name in the format of BucketName-APPID
String
Key
ObjectKey is the unique identifier of the object in the bucket. For example, in the object's access domain name examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/doc/pic.jpg, the ObjectKey is doc/pic.jpg
String

Uploading parts

This API (Upload Part) is used to upload parts in a multipart upload.

Method prototype

upload_part(Bucket, Key, Body, PartNumber, UploadId, **kwargs)

Sample Request

# -*- 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. To locate issues, change it to DEBUG, and 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, etc. 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 <1>SecretId</1>. We recommend that you use a sub-account key and follow the principle of least privilege to reduce risks. For more information on how to obtain a sub-account key, visit https://cloud.tencent.com/document/product/598/37140.
secret_key = os.environ['COS_SECRET_KEY'] # User <1>SecretKey</1>. We recommend that you use a sub-account key and follow the principle of least privilege to reduce risks. For more information on how to obtain a sub-account key, visit https://cloud.tencent.com/document/product/598/37140.
region = 'ap-beijing' # Replace it with the actual region, which can be viewed in the console at https://console.cloud.tencent.com/cos5/bucket.
For a list of all regions supported by COS, visit https://cloud.tencent.com/document/product/436/6224
token = None # Token is required for temporary keys but not permanent keys. For more information about how to generate and use a temporary key, see https://cloud.tencent.com/document/product/436/14048.
scheme = 'https' # Specify whether to use HTTP or HTTPS protocol to access COS. This is optional and is https by default.

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

# Note that the maximum number of upload parts is 10,000.
response = client.upload_part(
Bucket='examplebucket-1250000000',
Key='exampleobject',
Body=b'b'*1024*1024,
PartNumber=1,
UploadId='exampleUploadId'
)

Sample request with all parameters

# Note that the maximum number of upload parts is 10,000.
response = client.upload_part(
Bucket='examplebucket-1250000000',
Key='exampleobject',
Body=b'bytes'|file,
PartNumber=1,
UploadId='string',
EnableMD5=False|True,
ContentLength='123',
ContentMD5='string',
TrafficLimit='1048576'
)

Description

Parameter name
ParameterDescription
Local Disk Types
Required
Bucket
Bucket name in the format of BucketName-APPID
String
Required
Key
ObjectKey is the unique identifier of the object in the bucket. For example, in the object's access domain name examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/doc/pic.jpg, the ObjectKey is doc/pic.jpg
String
Required
Body
Content of the uploaded part, which can be file stream or byte stream
file/bytes
Required
PartNumber
Number that identifies the uploaded part
Int
Required
UploadId
ID that identifies the multipart upload
String
Required
EnableMD5
Specifies whether the SDK needs to calculate the Content-MD5 value. This feature is disabled by default. The upload will take longer if it is enabled
Bool
Not required
ContentLength
Length of the content
String
Not required
ContentMD5
Sets the MD5 checksum of the uploaded object
String
Not required
TrafficLimit
The single-link speed limit value is in bits per second (bit/s), with a range of 819200 to 838860800, which is equivalent to 800 Kb/s to 800 Mb/s.
String
Not required

Response description

This response contains the attributes of the uploaded parts in dict format:
{
'ETag': 'string'
}
Parameter name
ParameterDescription
Local Disk Types
ETag
MD5 checksum of the uploaded part
String

Copying a part

This API is used to upload a part by copying data from an existing object as data source.

Method prototype

upload_part_copy(Bucket, Key, PartNumber, UploadId, CopySource, CopySourceRange='', **kwargs)

Sample Request

# -*- 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. To locate issues, change it to DEBUG, and 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, etc. 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 <1>SecretId</1>. We recommend that you use a sub-account key and follow the principle of least privilege to reduce risks. For more information on how to obtain a sub-account key, visit https://cloud.tencent.com/document/product/598/37140.
secret_key = os.environ['COS_SECRET_KEY'] # User <1>SecretKey</1>. We recommend that you use a sub-account key and follow the principle of least privilege to reduce risks. For more information on how to obtain a sub-account key, visit https://cloud.tencent.com/document/product/598/37140.
region = 'ap-beijing' # Replace it with the actual region, which can be viewed in the console at https://console.cloud.tencent.com/cos5/bucket.
For a list of all regions supported by COS, visit https://cloud.tencent.com/document/product/436/6224
token = None # Token is required for temporary keys but not permanent keys. For more information about how to generate and use a temporary key, see https://cloud.tencent.com/document/product/436/14048.
scheme = 'https' # Specify whether to use HTTP or HTTPS protocol to access COS. This 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.upload_part_copy(
Bucket='examplebucket-1250000000',
Key='exampleobject',
PartNumber=1,
UploadId='exampleUploadId',
CopySource={
'Bucket': 'sourcebucket-1250000000',
'Key': 'exampleobject',
'Region': 'ap-guangzhou'
}
)

Sample request with all parameters

response = client.upload_part_copy(
Bucket='examplebucket-1250000000',
Key='exampleobject',
PartNumber=100,
UploadId='string',
CopySource={
'Bucket': 'sourcebucket-1250000000',
'Key': 'sourceObject',
'Region': 'COS_REGION', # Replace with the source bucket's Region
'VersionId': 'string'
},
CopySourceRange='string',
CopySourceIfMatch='string',
CopySourceIfModifiedSince='string',
CopySourceIfNoneMatch='string',
CopySourceIfUnmodifiedSince='string'
)

Description

Parameter name
ParameterDescription
Local Disk Types
Required
Bucket
Bucket name in the format of BucketName-APPID
String
Required
Key
ObjectKey is the unique identifier of the object in the bucket. For example, in the object's access domain name examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/doc/pic.jpg, the ObjectKey is doc/pic.jpg
String
Required
PartNumber
Number that identifies the uploaded part
Int
Required
UploadId
ID that identifies the multipart upload
String
Required
CopySource
Path of the source object to be copied, including Bucket, Key, Region, VersionId
Dict
Required
CopySourceRange
Byte range of the source object to copy in the format of bytes=first-last. The entire source will be copied by default if no range is specified.
String
Not required
CopySourceIfMatch
Copies the source object only if its Etag matches the specified value
String
Not required
CopySourceIfModifiedSince
Copy the source object only if it has been modified after the specified time.
String
Not required
CopySourceIfNoneMatch
Copies the source object only if its Etag does not match the specified value
String
Not required
CopySourceIfUnmodifiedSince
Copy the source object only if it has not been modified after the specified time.
String
Not required

Response description

This response contains the attributes of the copied parts in dict format:
{
'ETag': 'string',
'LastModified': 'string',
'x-cos-copy-source-version-id': 'string',
}
Parameter name
ParameterDescription
Local Disk Types
ETag
Copies the MD5 checksum of the part
String
LastModified
Last modified time of the copied part
String
x-cos-copy-source-version-id
Version ID of the source object
String

Querying uploaded parts

Note

This API is used to query the uploaded parts of a specific multipart upload operation.

Method prototype

list_parts(Bucket, Key, UploadId, MaxParts=1000, PartNumberMarker=0, EncodingType='', **kwargs)

Sample Request

# -*- 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. To locate issues, change it to DEBUG, and 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, etc. 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 <1>SecretId</1>. We recommend that you use a sub-account key and follow the principle of least privilege to reduce risks. For more information on how to obtain a sub-account key, visit https://cloud.tencent.com/document/product/598/37140.
secret_key = os.environ['COS_SECRET_KEY'] # User <1>SecretKey</1>. We recommend that you use a sub-account key and follow the principle of least privilege to reduce risks. For more information on how to obtain a sub-account key, visit https://cloud.tencent.com/document/product/598/37140.
region = 'ap-beijing' # Replace it with the actual region, which can be viewed in the console at https://console.cloud.tencent.com/cos5/bucket.
For a list of all regions supported by COS, visit https://cloud.tencent.com/document/product/436/6224
token = None # Token is required for temporary keys but not permanent keys. For more information about how to generate and use a temporary key, see https://cloud.tencent.com/document/product/436/14048.
scheme = 'https' # Specify whether to use HTTP or HTTPS protocol to access COS. This 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_parts(
Bucket='examplebucket-1250000000',
Key='exampleobject',
UploadId='exampleUploadId'
)

Sample request with all parameters

response = client.list_parts(
Bucket='examplebucket-1250000000',
Key='exampleobject',
UploadId=uploadid,
MaxParts=1000,
PartNumberMarker=100,
EncodingType='url'
)

Description

Parameter name
ParameterDescription
Local Disk Types
Required
Bucket
Bucket name in the format of BucketName-APPID
String
Required
Key
ObjectKey is the unique identifier of the object in the bucket. For example, in the object's access domain name examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/doc/pic.jpg, the ObjectKey is doc/pic.jpg
String
Required
UploadId
ID that identifies the multipart upload
String
Required
MaxParts
Maximum number of parts to return at a time. Default value: 1000
Int
Not required
PartNumberMarker
Specifies the part number after which the listing should begin. It defaults to 0, which means the listing begins with the first part
Int
Not required
EncodingType
Indicates the encoding method of the returned value. The value is not encoded by default. Valid value: url
String
Not required

Response description

This response contains the information of all the uploaded parts in DICT format:
{
'Bucket': 'examplebucket-1250000000',
'Key': 'exampleobject',
'UploadId': '1502192444bdb382add546a35b2eeab81e06ed84086ca0bb75ea45ca7fa073fa9cf74ec4f2',
'EncodingType': None,
'MaxParts': '1000',
'IsTruncated': 'true',
'PartNumberMarker': '0',
'NextPartNumberMarker': '1000',
'StorageClass': 'Standard',
'Part': [
{
'LastModified': '2017-08-08T11:40:48.000Z',
'PartNumber': '1',
'ETag': '"8b8378787c0925f42ccb829f6cc2fb97"',
'Size': '10485760'
},
],
'Initiator': {
'DisplayName': '3333333333',
'ID': 'qcs::cam::uin/3333333333:uin/3333333333'
},
'Owner': {
'DisplayName': '124564654654',
'ID': '124564654654'
}
}
Parameter name
ParameterDescription
Local Disk Types
Bucket
Bucket name in the format of BucketName-APPID
String
Key
ObjectKey is the unique identifier of the object in the bucket. For example, in the object's access domain name examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/doc/pic.jpg, the ObjectKey is doc/pic.jpg
String
UploadId
ID that identifies the multipart upload
String
EncodingType
Indicates the encoding method of the returned value. The value is not encoded by default. Valid value: url
String
MaxParts
Maximum number of parts to return at a time. Default value: 1000
String
IsTruncated
Indicates whether the returned list is truncated
String
PartNumberMarker
Specifies the part number after which the listing should begin. It defaults to 0, which means the listing begins with the first part
String
NextPartNumberMarker
Specifies the part number after which the next listing should begin
String
StorageClass
The storage types for objects include STANDARD, STANDARD_IA, and ARCHIVE. The default value is STANDARD. For more information on storage types, see Storage Class Overview.
String
Part
Information on the uploaded part, including ETag, PartNumber, Size, and LastModified
String
Initiator
Initiator of the multipart upload, including DisplayName and ID
Dict
Owner
Information on the object owner, including DisplayName and ID
Dict

Completing multipart upload

Note

This API (Complete Multipart Upload) is used to complete a multipart upload.

Method prototype

complete_multipart_upload(Bucket, Key, UploadId, MultipartUpload={}, **kwargs)

Sample Request

# -*- 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. To locate issues, change it to DEBUG, and 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, etc. 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 <1>SecretId</1>. We recommend that you use a sub-account key and follow the principle of least privilege to reduce risks. For more information on how to obtain a sub-account key, visit https://cloud.tencent.com/document/product/598/37140.
secret_key = os.environ['COS_SECRET_KEY'] # User <1>SecretKey</1>. We recommend that you use a sub-account key and follow the principle of least privilege to reduce risks. For more information on how to obtain a sub-account key, visit https://cloud.tencent.com/document/product/598/37140.
region = 'ap-beijing' # Replace it with the actual region, which can be viewed in the console at https://console.cloud.tencent.com/cos5/bucket.
For a list of all regions supported by COS, visit https://cloud.tencent.com/document/product/436/6224
token = None # Token is required for temporary keys but not permanent keys. For more information about how to generate and use a temporary key, see https://cloud.tencent.com/document/product/436/14048.
scheme = 'https' # Specify whether to use HTTP or HTTPS protocol to access COS. This 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.complete_multipart_upload(
Bucket='examplebucket-1250000000',
Key='exampleobject',
UploadId='exampleUploadId',
MultipartUpload={
'Part': [
{
'ETag': 'string',
'PartNumber': 1
},
{
'ETag': 'string',
'PartNumber': 2
},
]
},
)


Description

Parameter name
ParameterDescription
Local Disk Types
Required
Bucket
Bucket name in the format of BucketName-APPID
String
Required
Key
ObjectKey is the unique identifier of the object in the bucket. For example, in the object's access domain name examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/doc/pic.jpg, the ObjectKey is doc/pic.jpg
String
Required
UploadId
ID that identifies the multipart upload
String
Required
MultipartUpload
Information on all parts, including ETag and PartNumber
Dict
Required

Response description

The response contains information on the merged object in dict format:
{
'ETag': '"3f866d0050f044750423e0a4104fa8cf-2"',
'Bucket': 'examplebucket-1250000000',
'Location': 'examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/exampleobject',
'Key': 'exampleobject'
}
Parameter name
ParameterDescription
Local Disk Types
ETag
The unique tag of a merged object. This value is not necessarily the MD5 checksum of the object. It is used to verify the uniqueness of the merged object. To verify the object content, you can check the ETag of each part during the upload process.
String
Bucket
Bucket name in the format of BucketName-APPID
String
Location
URL
String
Key
ObjectKey is the unique identifier of the object in the bucket. For example, in the object's access domain name examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/doc/pic.jpg, the ObjectKey is doc/pic.jpg
String

Aborting a multipart upload

Note

This API (Abort Multipart Upload) is used to abort a multipart upload and delete the uploaded parts.

Method prototype

abort_multipart_upload(Bucket, Key, UploadId, **kwargs)

Sample Request

# -*- 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. To locate issues, change it to DEBUG, and 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, etc. 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 <1>SecretId</1>. We recommend that you use a sub-account key and follow the principle of least privilege to reduce risks. For more information on how to obtain a sub-account key, visit https://cloud.tencent.com/document/product/598/37140.
secret_key = os.environ['COS_SECRET_KEY'] # User <1>SecretKey</1>. We recommend that you use a sub-account key and follow the principle of least privilege to reduce risks. For more information on how to obtain a sub-account key, visit https://cloud.tencent.com/document/product/598/37140.
region = 'ap-beijing' # Replace it with the actual region, which can be viewed in the console at https://console.cloud.tencent.com/cos5/bucket.
For a list of all regions supported by COS, visit https://cloud.tencent.com/document/product/436/6224
token = None # Token is required for temporary keys but not permanent keys. For more information about how to generate and use a temporary key, see https://cloud.tencent.com/document/product/436/14048.
scheme = 'https' # Specify whether to use HTTP or HTTPS protocol to access COS. This 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.abort_multipart_upload(
Bucket='examplebucket-1250000000',
Key='exampleobject',
UploadId='exampleUploadId'
)

Description

Parameter name
ParameterDescription
Local Disk Types
Required
Bucket
Bucket name in the format of BucketName-APPID
String
Required
Key
ObjectKey is the unique identifier of the object in the bucket. For example, in the object's access domain name examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/doc/pic.jpg, the ObjectKey is doc/pic.jpg
String
Required
UploadId
ID that identifies the multipart upload
String
Required

Response description

This API returns None.