By default, if you upload an object that has the same name as an existing object, the existing object is overwritten by the uploaded object. This topic describes how to set the x-oss-forbid-overwrite request header to disable overwrite for objects with the same name when you copy objects or use the simple upload and multipart upload methods.

Simple upload

The following code provides an example on how to disable overwrite for an object with the same name when you use simple upload:

const OSS = require("ali-oss")

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

async function put () {
  try {
    // Set object-name to the object name such as file.text or the directory such as abc/test/file.text to upload the object to the current bucket or the specified directory.
    // Specify whether to overwrite the object with the same name when you call the PutObject operation.
    // By default, if x-oss-forbid-overwrite is not specified, the object with the same name is overwritten.
    // If x-oss-forbid-overwrite is set to false, the object with the same name is overwritten.
    // If x-oss-forbid-overwrite is set to true, the object with the same name is not overwritten. If an object with the same name already exists, an error is reported.
    let result = await client.put('object-name', 'local-file',{headers: { 'x-oss-forbid-overwrite': true }});
    console.log(result);
  } catch (e) {
    console.log(e);
  }
}

put();

For more information about simple upload, see PutObject.

Copy objects

  • Copy a small object

    The following code provides an example on how to copy a small object without overwriting the object with the same object name:

    const OSS = require("ali-oss")
    
    const client = new OSS({
      region: '<Your region>',
      accessKeyId: '<Your AccessKeyId>',
      accessKeySecret: '<Your AccessKeySecret>',
      bucket: '<Your bucket name>',
    });
    
    // Copy objects from the same bucket.
    // Specify whether to overwrite the object with the same name.
    // By default, if x-oss-forbid-overwrite is not specified, the object with the same name is overwritten.
    // If x-oss-forbid-overwrite is set to false, the object with the same name is overwritten.
    // If x-oss-forbid-overwrite is set to true, the object with the same name is not overwritten. If an  object with the same name already exists, an error is reported.
    client
      .copy("test_copy", "test", { headers: { "x-oss-forbid-overwrite": true } })
      .then(res => {
        console.log(res);
      })
      .catch(e => {
        console.log(e);
      });
  • Copy a large object

    The following code provides an example on how to disable overwrite for the object with the same name when you copy a large object by using multipart copy:

    const client = new OSS({
      region: '<Your region>',
      accessKeyId: '<Your AccessKeyId>',
      accessKeySecret: '<Your AccessKeySecret>',
      bucket: '<Your bucket name>',
    });
    
    async function put () {
      try {
        let result = await client.multipartUploadCopy(
          copyName,
          {
            sourceKey: objectkey, // Specify the name of the source object to copy.
            sourceBucketName: bucket // Specify the name of the source bucket.
          },
          {
           // Specify whether to overwrite the object with the same name.
           // By default, if x-oss-forbid-overwrite is not specified, the object with the same name is overwritten.
           // If x-oss-forbid-overwrite is set to false, the object with the same name is overwritten.
           // If x-oss-forbid-overwrite is set to true, the object with the same name is not overwritten. If an object with the same name already exists, an error is reported.
            headers: { "x-oss-forbid-overwrite": true }
          }
        );
        console.log(result);
      } catch (e) {
        console.log(e);
      }
    }

For more information about how to copy an object, see CopyObject.

Multipart upload

The following code provides an example on how to disable overwrite for the object with the same name when you use multipart upload:

const OSS = require("ali-oss")

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

// Specify whether to overwrite the object with the same name.
// By default, if x-oss-forbid-overwrite is not specified, the object with the same name is overwritten.
// If x-oss-forbid-overwrite is set to false, the object with the same name is overwritten.
// If x-oss-forbid-overwrite is set to true, the object with the same name is not overwritten. If an  object with the same name already exists, an error is reported.
 client
  .multipartUpload('object-name', 'local-file', { headers: { "x-oss-forbid-overwrite": true } })
  .then(res => {
    console.log(res);
  })
  .catch(e => {
    console.log(e);
  });

For more information about multipart upload, see InitiateMultipartUpload and CompleteMultipartUpload.