You can use the CopyObject operation to copy an object between the same or different buckets in 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, which indicates that the object does not exist.
You can restore an earlier version of an object as the current version by copying the earlier version to the same bucket. OSS then sets that earlier version as the 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.
Limits
Object size limits
If the source and destination buckets are the same, and you do not change the encryption method or storage class of the object during the copy operation, the object can be larger than 5 GB.
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 encryption method or storage class of the object during the copy operation, the object cannot be larger than 1 GB. If the object is larger than 1 GB, you must use the UploadPartCopy operation.
Permissions
Both the CopyObject and UploadPartCopy operations require read permissions 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 file to which the symbolic link points is not copied.
Hierarchical namespace is enabled for the bucket
If hierarchical namespace is enabled for the bucket, you cannot copy directories.
Preventing file overwrite conflicts
If you enable Prevent File Overwrite, you cannot use CopyObject to change a file's storage class, such as from Standard to Archive Storage. Instead, use automatic lifecycle conversion.
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 | Definition |
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. | |
|
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.
Changing the storage class of an object using the CopyObject operation involves data overwrites. 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.
When you call the CopyObject operation, if the source object is an IA object, a data retrieval fee for IA storage is incurred. If the source object is an Archive Storage object that has not been restored using RestoreObject and real-time access of Archive objects is enabled for the bucket, 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-. Therefore, you must add all these request headers to the signature string.
Name | Type | Required | Value | Description |
x-oss-forbid-overwrite | String | No | true | Specifies whether to overwrite an existing 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 invalid. This means that overwriting an object with the same name is allowed.
Setting the x-oss-forbid-overwrite request header degrades QPS processing performance. To use the x-oss-forbid-overwrite request header for many operations (QPS > 1000), contact technical support to avoid affecting your business. Default value: false |
x-oss-copy-source | String | Yes | /oss-example/oss.jpg | Specifies the source address for the copy operation. Default value: none |
x-oss-copy-source-if-match | String | No | 5B3C1A2E053D763E1B002CC607C5**** | The copy operation is performed and 200 OK is returned only if the ETag of the source object matches the ETag you provide. Default value: none |
x-oss-copy-source-if-none-match | String | No | 5B3C1A2E053D763E1B002CC607C5**** | The copy operation is performed and 200 OK is returned only if the ETag of the source object does not match the ETag you provide. Default value: none |
x-oss-copy-source-if-unmodified-since | String | No | Mon, 11 May 2020 08:16:23 GMT | The object is copied and 200 OK is returned only if the time you specify is the same as or later than the actual modification time of the object. Default value: none |
x-oss-copy-source-if-modified-since | String | No | Mon, 11 May 2020 08:16:23 GMT | The object is copied and 200 OK is returned only if the time you specify is earlier than the actual modification time of the object. Default value: none |
x-oss-metadata-directive | String | No | COPY | Specifies how to set the metadata of the destination object.
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 to create 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 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 permissions of the destination object when it is created in OSS. 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 the tags of the object. You can specify multiple tags at the same time, for example, TagA=A&TagB=B. Note The key and value must be URL-encoded. If an item does not contain an equal sign (=), the value is considered an empty string. |
x-oss-tagging-directive | String | No | Copy | Specifies how to set the tags of the destination object. Valid values:
|
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
Name | 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
x-oss-hash-crc64ecmaindicates the 64-bit CRC value of the object. This 64-bit CRC 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 in this case is the current version.x-oss-version-idis 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.x-oss-version-idis 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 software development kits (SDKs) for the following languages to call this operation.
ossutil command-line tool
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. |
Precondition Failed | 412 | This error is returned for one of the following reasons:
|
Not Modified | 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 if you try to delete or modify data in a bucket that is in a protected state. |
FAQ
Does CopyObject support batch copying of files?
No, it does not. The CopyObject operation is used to copy a single file. To copy multiple files in a batch, you can use ossutil. For more information, see cp (copy files).