Feature Overview
This document provides an overview of APIs and SDK code samples related to object copy and movement.
API | Operation | Description |
Setting object replication | Copies an object to the destination path |
Advanced APIs (Recommended)
Copying an object
Note
This advanced API copies objects smaller than 5 GB by calling
copy_object. If an object is larger than or equal to 5 GB, it calls upload_part_copy.Method prototype
copy(Bucket, Key, CopySource, CopyStatus='Copy', PartSize=10, MAXThread=5, **kwargs)
Sample 1. Copying an object
# -*- coding=utf-8from qcloud_cos import CosConfigfrom qcloud_cos import CosS3Clientimport sysimport osimport 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/6224token = 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.copy(Bucket='examplebucket-1250000000',Key='exampleobject',CopySource={'Bucket': 'sourcebucket-1250000000','Key': 'sourceobject','Region': 'ap-guangzhou'})
Sample 2: moving an object
# -*- coding=utf-8from qcloud_cos import CosConfigfrom qcloud_cos import CosS3Clientimport sysimport osimport 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/6224token = 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)bucket = 'examplebucket-1250000000'srcKey = 'src_object_key' # Path of the source objectdestKey = 'dest_object_key' # Path of the destination object# COS does not have a dedicated interface for moving objects. The so-called move operation involves copying the old object to a new object and then deleting the old object.response = client.copy(Bucket=bucket,Key=destKey,CopySource={'Bucket':bucket,'Key':srcKey,'Region':'ap-guangzhou',})client.delete_object(Bucket=bucket, Key=srcKey)
Sample request with all parameters
response = client.copy(Bucket='examplebucket-1250000000',Key='exampleobject',CopySource={'Bucket': 'sourcebucket-1250000000','Key': 'exampleobject','Region': 'ap-guangzhou'}CopyStatus='Copy'|'Replaced',PartSize=20,MAXThread=10)
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 |
CopySource | Path of the source object to be copied, including Bucket, Key, Region, VersionId | Dict | Required |
CopyStatus | Copy status. Valid values: Copy, Replaced | String | Not required |
PartSize | Part size for multipart download. Default value: 10 MB | Int | Not required |
MAXThread | Maximum number of concurrent threads for a multipart download. Default value: 5 | Int | Not required |
Response description
If the object is smaller than 5 GB, it will be the result of
copy_object of the dict format. Otherwise, it will be the result of complete_multipart_upload.Simple Operations
Copying an object
Note
This API is used to copy an existing object to a destination path.
Method prototype
copy_object(Bucket, Key, CopySource, CopyStatus='Copy', **kwargs)
Sample 1. Copying an object
If the source and destination objects are different, a new object will be generated. The metadata of the destination object will be copied from the source.
# -*- coding=utf-8from qcloud_cos import CosConfigfrom qcloud_cos import CosS3Clientimport sysimport osimport 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/6224token = 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.copy_object(Bucket='examplebucket-1250000000',Key='exampleobject',CopySource={'Bucket': 'sourcebucket-1250000000','Key': 'sourceobject','Region': 'ap-guangzhou'})
Sample 2: moving an object
COS does not offer an API to move objects. To move an object, you can copy it first and delete the old one.
# -*- coding=utf-8from qcloud_cos import CosConfigfrom qcloud_cos import CosS3Clientimport sysimport osimport 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/6224token = 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.copy_object(Bucket='examplebucket-1250000000',Key='exampleobject',CopySource={'Bucket': 'sourcebucket-1250000000','Key': 'sourceobject','Region': 'ap-guangzhou'})client.delete_object(Bucket='sourcebucket-1250000000', Key='sourceobject')
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 |
CopySource | Path of the source object to be copied, including Bucket, Key, Region, VersionId | Dict | Required |
CopyStatus | Valid values: Copy: ignores the configured metadata and copy the file directly. Replaced: modifies the metadata according to the configured metadata. If the destination path and source path are the same, this parameter must be set to Replaced | String | Required |
ACL | Sets the object ACL, such as private or public-read | String | Not required |
GrantFullControl | Grants full permission in the format: id="OwnerUin" | String | Not required |
GrantRead | Grants read permission in the format: id="OwnerUin" | String | Not required |
StorageClass | Sets the object storage class. Enumerated values: STANDARD (default) or STANDARD_IA. | 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 |
Response description
The response contains the attributes of the uploaded object in dict format:
{'ETag': 'string','LastModified': 'string','VersionId': 'string','x-cos-copy-source-version-id': 'string'}
Parameter name | ParameterDescription | Local Disk Types |
ETag | Copies the MD5 checksum of the object | String |
LastModified | Last modified time of the copied object | String |
VersionId | Version IDs of the objects to be deleted if versioning is enabled. | String |
x-cos-copy-source-version-id | Version ID of the source object | String |