This topic describes how to copy an object.

You can use copy to copy an object in either of the following situations:

  • The source and destination objects are in the same bucket.
  • The source and destination objects are in different buckets within the same region. The source object name must be in the format of '/Bucket/object.'

Additionally, you can process Object Meta as follows when you copy an object:

  • If meta is not specified, the Object Meta of the source object is copied.
  • If meta is specified, the specified Object Meta overwrites the Object Meta of the source object.

Use the following code to copy an object:

let OSS = require('ali-oss')

let client = new OSS({
  region: '<Your region>',
  accessKeyId: '<Your AccessKeyId>',
  accessKeySecret: '<Your AccessKeySecret>',
  bucket: 'Your bucket name'
});

async function copy () {
  try {
     // Copy the object from a bucket to another bucket.
    let result = await client.copy('to', '/from-bucket/from');
    console.log(result);

    // Copy Object Meta.
    let result = await client.copy('to', 'from');
    console.log(result);

    // Overwrite Object Meta.
    let result = await client.copy('to', 'from', {
      meta: {
        year: 2015,
        people: 'mary'
      }
    });
    console.log(result);
  } catch (e) {
    console.log(e);
  }
}