You can use the CP command to copy objects from your OSS to other folders or buckets. Only objects can be copied. Parts cannot be copied. You cannot copy objects across regions.

Note For more information about the format and supported parameters of the cp command, see Overview.

Sample environment

In this topic, objects are copied between different folders or buckets in Linux. You can modify the corresponding parameters based on your operating system and environment. Sample environment:

  • Operating system: Linux
  • Source bucket: examplebucket1
  • Folder 1 specified by the source bucket: srcfolder1
  • Folder 2 specified by the source bucket: srcfolder2
  • Source object: examplefile.txt
  • Destination bucket: examplebucket2
  • Folder specified by the destination bucket: desfolder

Example for simple copy

  • Copy a single object
    ./ossutil cp oss://examplebucket1/examplefile.txt oss://examplebucket1/srcfolder2/                                 
  • Copy a folder
    You can add the -r option to the cp command to copy multiple objects between different buckets within the same region.
    ./ossutil cp oss://examplebucket1/srcfolder1/ oss://examplebucket2/desfolder/ -r                                   
  • Perform incremental copy on objects
    If you specify the --update option when you copy multiple objects, ossutil copies only objects that do not exist in the destination bucket or objects whose modified time is earlier than that of their corresponding objects in the destination bucket. Command:
    ./ossutil cp oss://examplebucket1/srcfolder1/ oss://examplebucket2/path2/ -r --update
    This option can be used to perform incremental copy on objects that fail to be copied.
  • Rename objects
    ./ossutil cp oss://examplebucket1/examplefile.txt oss://examplebucket1/example.txt                        
    When you rename an object by using the cp command, the original object still exists. You can delete the original object after rename the object.
  • Copy only objects in the current folder and ignore subfolders
    ./ossutil cp oss://examplebucket1/srcfolder1/ oss://examplebucket1/srcfolder2/ --only-current-dir -r
  • Modify objects
    When you copy an object, you can use the --meta option to configure object metadata in the header:value#header:value... format.
    ./ossutil cp oss://examplebucket1/examplefile.txt oss://examplebucket1/ --meta=Cache-Control:no-cache
  • Copy an object from a bucket that has pay-by-requester enabled to a common bucket
    ./ossutil cp oss://examplebucket1/examplefile.txt oss://examplebucket2/desfolder/  --payer=requester

Modify the storage class of objects

When you overwrite an object, you can use the --meta option to modify the storage class of the object. Valid values:
  • Standard
  • IA (Infrequent access)
  • Archive
For more information, see Overview. Command:
  • Set the storage class of a specified object to Archive
    ./ossutil cp oss://examplebucket1/srcfolder1/examplefile.txt oss://examplebucket1/srcfolder1/examplefile.txt --meta X-oss-Storage-Class:Archive
  • Set the storage class of all objects in the specified folder to Standard
    ./ossutil cp oss://examplebucket1/srcfolder1/ oss://examplebucket1/srcfolder1/ --meta X-oss-Storage-Class:Standard -r
    • You cannot use the cp command to convert the storage class of an object from Archive or Clod Archive to any other storage class. You must first use the restore command to restore the object to the readable status, and then use the cp command to modify its storage class.
    • When you run the cp command to overwrite an object, fees may be incurred. If an object of the IA, Archive, and Cold Archive storage class has been stored for less than the minimum storage duration and is overwritten, you are charged for the minimum storage duration, including the remaining duration. For more information, see Storage fees.

Modify object tags

You can add the --tagging option to modify object tags when you overwrite objects. Separate multiple tags with ampersands (&). Command:
./ossutil cp oss://examplebucket1/examplefile.txt oss://examplebucket1/ --tagging "abc=1&bcd=2&……"

For more information about object tagging, see object-tagging.

Copy and encrypt objects

You can specify the server-side encryption method when you copy an object and store the encrypted object in a bucket. For more information about server-side encryption, see Server-side encryption.
  • Copy an object and set the server-side encryption method to AES-256
    ./ossutil cp oss://examplebucket1/examplefile.txt oss://examplebucket1/srcfolder2/ --meta=x-oss-server-side-encryption:AES256
  • Copy an object and set the server-side encryption method to KMS
    ./ossutil cp oss://examplebucket1/examplefile.txt oss://examplebucket2/desfolder/ --meta=x-oss-server-side-encryption:KMS
    Notice When you use KMS to encrypt an object, OSS creates a customer master key (CMK) in the KMS console for the object and incurs a small amount of fees when the KMS API operation is called. For more information, see KMS billing methods.

Restore objects in versioning-enabled buckets

When versioning is enabled for a bucket, data that is overwritten or deleted is saved as a previous version. You can add the --version-id option to the cp command to overwrite the previous version as the latest version. Command:
./ossutil cp oss://examplebucket1/examplefile.txt oss://examplebucket2/ --version-id  CAEQARiBgID8rumR2hYiIGUyOTAyZGY2MzU5MjQ5ZjlhYzQzZjNlYTAyZDE3MDRk
  • To use the --version-id option, you must run the ls --all-versions command to query version IDs of the object.
  • The --version-id option can be used for objects only in versioning-enabled buckets. For more information about the command used to enable versioning on a bucket, see bucket-versioning.

Copy multiple objects that meet specified conditions

When you copy multiple objects, you can use the --include and --exclude options to specify conditions to match objects. For more information, see Upload multiple objects that meet specified conditions.
  • Copy all objects that are not in the JPG format
    ./ossutil cp oss://examplebucket1/srcfolder1/ oss://examplebucket2/desfolder/ --exclude "*.jpg" -r
  • Copy all objects that contain abc in their names and are not in the JPG or TXT format
    ./ossutil cp oss://examplebucket1/srcfolder1/ oss://examplebucket2/desfolder/ --include "*abc*" --exclude "*.jpg" --exclude "*.txt" -r