Use the CopyObject operation to copy an object between the same or different buckets within the same region.
Versioning
By default, x-oss-copy-source copies the current version of an object. To copy a specific version, include the version ID in x-oss-copy-source. If the specified source version is a delete marker, OSS returns a 404 error, indicating the object does not exist.
To restore a previous version of an object, copy that version to the same bucket. This action makes the copied version the new current version.
If versioning is enabled for the destination bucket, OSS automatically generates a unique version ID for the newly copied object. This version ID is returned in the x-oss-version-id response header. If versioning is disabled or suspended for the destination bucket, OSS generates a version with a null version ID for the new object. This new version overwrites any existing version that has a null version ID.
Limitations
Object size limits
Objects larger than 5 GB can be copied within the same bucket if you do not change the encryption method or storage class.
If the source and destination buckets are different, and you do not change the encryption method or storage class of the object during the copy operation, the object cannot be larger than 5 GB.
If you change the object's encryption method or storage class during the copy, the object size is limited to 1 GB. For larger objects, you must use the UploadPartCopy operation.
Permission
Both the CopyObject and UploadPartCopy operations require read permission on the source object.
If you use the CopyObject operation in a bucket where versioning is disabled and the source and destination objects are the same:
If the encryption method or storage class is not changed, OSS modifies only the metadata of the object and does not copy its content.
If the encryption method or storage class is changed, OSS modifies the metadata and also copies the content of the object.
Source object is a symbolic link
If you use the CopyObject operation on a symbolic link, only the symbolic link is copied. The content of the object to which the symbolic link points is not copied.
Hierarchical namespace is enabled for the bucket
You cannot copy directories within a bucket for which hierarchical namespace is enabled.
Preventing object overwrite conflicts
If you enable Prevent File Overwrite, you cannot use CopyObject to change an object's storage class, such as from Standard to Archive Storage. Instead, use a lifecycle rule to automatically transition the storage class.
ObjectWorm limitations
When you copy an appendable object, you cannot include ObjectWorm-related parameters in the request.
Permissions
By default, an Alibaba Cloud account has full permissions. RAM users or RAM roles under an Alibaba Cloud account do not have any permissions by default. The Alibaba Cloud account or account administrator must grant operation permissions through RAM policies or Bucket Policy.
|
API |
Action |
Description |
|
CopyObject |
|
Copies objects within a bucket or between buckets in the same region. |
|
|
||
|
|
If you specify the source object version through versionId, this permission is also required. |
|
|
|
If you copy object tags through x-oss-tagging, these permissions are required. |
|
|
|
||
|
|
If you specify the tags of a specific version of the source object through versionId, this permission is also required. |
|
|
|
When copying an object, if the destination object metadata contains X-Oss-Server-Side-Encryption: KMS, these two permissions are required. |
|
|
|
If ObjectWorm is enabled for the destination bucket, the copied object inherits the bucket's default retention policy. You can specify a separate retention policy for the destination object by using the x-oss-object-worm-mode and x-oss-object-worm-retain-until-date request headers. The retention policy of the source object is not copied to the destination object.
Billing
Each call to the CopyObject operation counts as one PUT request for the destination bucket.
The CopyObject operation increases the storage usage of the destination bucket.
Using CopyObject to change an object's storage class is a data overwrite operation. For example, if an Infrequent Access (IA) object is overwritten and its storage class is changed to Standard within 10 days of its creation, a fee is charged for 20 days of IA storage because the minimum storage duration was not met. For more information about storage fees, see Storage fees.
Calling the CopyObject operation on an Infrequent Access (IA) object incurs a data retrieval fee for IA storage. If the source object is an unrestored Archive Storage object in a bucket with real-time access enabled, a data retrieval fee for real-time access is incurred. These fees are charged to the account that owns the source bucket. For more information about billing, see Data processing fees.
Request syntax
PUT /DestObjectName HTTP/1.1
Host: DestBucketName.oss-cn-hangzhou.aliyuncs.com
Date: GMT Date
Authorization: SignatureValue
x-oss-copy-source: /SourceBucketName/SourceObjectNameRequest headers
All request headers for a copy operation start with x-oss- and must be included in the signature string.
Parameter | Type | Required | Value | Description |
x-oss-forbid-overwrite | String | No | true | Specifies whether to overwrite a destination object that has the same name. If versioning is enabled or suspended for the destination bucket, the x-oss-forbid-overwrite request header is ignored and the object is allowed to be overwritten.
Setting the x-oss-forbid-overwrite request header degrades QPS performance. If you need to perform a large number of operations that use the x-x-oss-forbid-overwrite request header (QPS>1000), contact technical support to avoid disruptions to your business. Default value: false |
x-oss-copy-source | String | Yes | /oss-example/oss.jpg | Specifies the source path for the copy operation. Default value: None |
x-oss-copy-source-if-match | String | No | 5B3C1A2E053D763E1B002CC607C5**** | The operation proceeds only if the source object's ETag matches the specified ETag. Default value: None |
x-oss-copy-source-if-none-match | String | No | 5B3C1A2E053D763E1B002CC607C5**** | The operation proceeds only if the source object's ETag does not match the specified ETag. Default value: None |
x-oss-copy-source-if-unmodified-since | String | No | Mon, 11 May 2020 08:16:23 GMT | Copies the object only if it has not been modified since the specified time. Default value: None |
x-oss-copy-source-if-modified-since | String | No | Mon, 11 May 2020 08:16:23 GMT | Copies the object only if it has been modified since the specified time. Default value: None |
x-oss-metadata-directive | String | No | COPY | Specifies whether to copy the source object's metadata or replace it.
Important If the source and destination objects are the same and versioning is not enabled, the metadata of the source object is ignored regardless of the value of x-oss-metadata-directive. The destination object uses the metadata specified in the request. |
x-oss-server-side-encryption | String | No | AES256 | Specifies the server-side encryption algorithm that OSS uses when it creates the destination object. Valid values: AES256 and KMS Important You cannot specify x-oss-server-side-encryption when you copy a symbolic link object. You can use the KMS encryption algorithm only after you purchase a KMS suite. Otherwise, OSS returns the KmsServiceNotEnabled error.
|
x-oss-server-side-encryption-key-id | String | No | 9468da86-3509-4f8d-a61e-6eab1eac**** | Specifies the ID of the customer master key (CMK) that is managed by KMS. This parameter is valid only when x-oss-server-side-encryption is set to KMS. |
x-oss-object-acl | String | No | private | Specifies the access control list (ACL) for the destination object when it is created. Valid values:
For more information about access permissions, see Object ACL. |
x-oss-storage-class | String | No | Standard | Specifies the storage class of the object. For a bucket of any storage class, if you specify this header when you upload an object, the uploaded object is stored in the specified storage class. For example, if you set x-oss-storage-class to Standard when you upload an object to an IA bucket, the object is stored as a Standard object. Valid values:
For more information about storage classes, see Storage classes. |
x-oss-tagging | String | No | a:1 | Specifies tags for the object. To specify multiple tags, use an ampersand (&) as a separator, for example: Note The key and value must be URL-encoded. If a tag does not contain an equal sign (=), its value is considered an empty string. |
x-oss-tagging-directive | String | No | Copy | Specifies how to set the tags of the destination object.
|
x-oss-object-worm-mode | String | No | COMPLIANCE | Specifies the retention policy mode for the destination object. Valid value: COMPLIANCE. This header is valid only if ObjectWorm is enabled for the destination bucket. |
x-oss-object-worm-retain-until-date | String | No | 2025-01-01T00:00:00.000Z | Specifies the retention expiration date for the destination object. The date must be in ISO 8601 format. This header is valid only if ObjectWorm is enabled for the destination bucket. |
This operation also uses common request headers, such as Host and Date. For more information, see Common Request Headers.
Response headers
This operation uses only common response headers. For more information, see Common Response Headers.
Response elements
Parameter | Type | Example | Description |
CopyObjectResult | Container | N/A | A container for the results of the CopyObject operation. Default value: None |
ETag | String | 5B3C1A2E053D763E1B002CC607C5**** | The ETag of the destination object. Parent element: CopyObjectResult |
LastModified | String | Fri, 24 Feb 2012 07:18:48 GMT | The time when the destination object was last updated. Parent element: CopyObjectResult |
Examples
Versioning is disabled
Sample request
PUT /test%2FAK.txt HTTP/1.1 Host: tesx.oss-cn-zhangjiakou.aliyuncs.com Accept-Encoding: identity User-Agent: aliyun-sdk-python/2.6.0(Windows/7/AMD64;3.7.0) Accept: text/html Connection: keep-alive x-oss-copy-source: /test/AK.txt date: Fri, 28 Dec 2018 09:41:55 GMT authorization: OSS4-HMAC-SHA256 Credential=LTAI********************/20250417/cn-hangzhou/oss/aliyun_v4_request,AdditionalHeaders=content-length,Signature=a7c3554c729d71929e0b84489addee6b2e8d5cb48595adfc51868c299c0c218e Content-Length: 0Sample response
The
x-oss-hash-crc64ecmaheader indicates the 64-bit CRC value of the object. This value is calculated based on the CRC-64/XZ standard. The CopyObject operation does not guarantee that the generated object has a 64-bit CRC value.HTTP/1.1 200 OK Server: AliyunOSS Date: Fri, 28 Dec 2018 09:41:56 GMT Content-Type: application/xml Content-Length: 184 Connection: keep-alive x-oss-request-id: 5C25EFE4462CE00EC6D87156 ETag: "F2064A169EE92E9775EE5324D0B1****" x-oss-hash-crc64ecma: 12753002859196105360 x-oss-server-time: 150 <?xml version="1.0" encoding="UTF-8"?> <CopyObjectResult> <ETag>"F2064A169EE92E9775EE5324D0B1****"</ETag> <LastModified>2018-12-28T09:41:56.000Z</LastModified> </CopyObjectResult>Copy an object without specifying a version ID
Sample request
PUT /dest-object-example HTTP/1.1 Host: versioning-copy.oss-cn-hangzhou.aliyuncs.com Date: Tue, 09 Apr 2019 03:45:32 GMT Authorization: OSS4-HMAC-SHA256 Credential=LTAI********************/20250417/cn-hangzhou/oss/aliyun_v4_request,Signature=a7c3554c729d71929e0b84489addee6b2e8d5cb48595adfc51868c299c0c218e x-oss-copy-source: /versioning-copy-source/source-objectSample response
In this example,
x-oss-copy-source-version-idis the version ID of the source object, which is the current version. Thex-oss-version-idheader contains the version ID of the newly copied object.HTTP/1.1 200 OK x-oss-copy-source-version-id: CAEQNRiBgIC28uaA0BYiIDY5OGIwNmNlNjYyMTRjNTc4N2M2OGNiMjZkZTQ2**** x-oss-version-id: CAEQNxiBgIDG8uaA0BYiIGZhZDRkZTk5Zjg3YzRhNzdiMWEwZGViNDM1NTFh**** x-oss-request-id: 5CAC155CB7AEADE01700**** Content-Type: application/xml Content-Length: 184 Connection: keep-alive Date: Tue, 09 Apr 2019 03:45:32 GMT Server: AliyunOSS <?xml version="1.0" encoding="UTF-8"?> <CopyObjectResult> <ETag>"C81E728D9D4C2F636F067F89CC14****"</ETag> <LastModified>2019-04-09T03:45:32.000Z</LastModified> </CopyObjectResult>Copy an object by specifying a version ID
Sample request
PUT /dest-object-example HTTP/1.1 Host: versioning-copy.oss-cn-hangzhou.aliyuncs.com Date: Tue, 09 Apr 2019 03:45:32 GMT Authorization: OSS4-HMAC-SHA256 Credential=LTAI********************/20250417/cn-hangzhou/oss/aliyun_v4_request,Signature=a7c3554c729d71929e0b84489addee6b2e8d5cb48595adfc51868c299c0c218e x-oss-copy-source: /versioning-copy-source/source-object?versionId=CAEQNRiBgICv8uaA0BYiIDliZDc3MTc1NjE5MjRkMDI4ZGU4MTZkYjY1ZDgy****Sample response
In this example,
x-oss-copy-source-version-idis the version ID of the source object, which is the version specified in thex-oss-copy-sourcerequest header. Thex-oss-version-idheader contains the version ID of the newly copied object.HTTP/1.1 200 OK x-oss-copy-source-version-id: CAEQNRiBgICv8uaA0BYiIDliZDc3MTc1NjE5MjRkMDI4ZGU4MTZkYjY1ZDgy**** x-oss-version-id: CAEQNxiBgMDP8uaA0BYiIDIyNGNhZDQ1M2M3NzRkZThiNzE0N2I3ZDkxOWY4**** x-oss-request-id: 5CAC155CB7AEADE01700**** Content-Type: application/xml Content-Length: 184 Connection: keep-alive Date: Tue, 09 Apr 2019 03:45:32 GMT Server: AliyunOSS <?xml version="1.0" encoding="UTF-8"?> <CopyObjectResult> <ETag>"C4CA4238A0B923820DCC509A6F75****"</ETag> <LastModified>2019-04-09T03:45:32.000Z</LastModified> </CopyObjectResult>
SDKs
You can use the SDKs for the following languages to call this operation:
ossutil CLI
For the ossutil command that corresponds to the CopyObject operation, see copy-object.
Error codes
Error code | HTTP status code | Description |
InvalidArgument | 400 | The value of a parameter, such as x-oss-storage-class, is invalid. |
PreconditionFailed | 412 | This error is returned for one of the following reasons:
|
NotModified | 304 | This error is returned for one of the following reasons:
|
KmsServiceNotEnabled | 403 | You set x-oss-server-side-encryption to KMS, but you have not purchased a KMS suite. |
FileAlreadyExists | 409 | This error is returned for one of the following reasons:
|
FileImmutable | 409 | This error is returned when you attempt to modify an object that is protected by an ObjectWorm retention policy. |
FAQ
Does CopyObject support copying multiple objects at once?
The CopyObject operation copies only a single object. To copy multiple objects in a batch, use the ossutil command-line tool.