All Products
Search
Document Center

Object Storage Service:Prevent overwriting objects with the same name (Node.js SDK)

Last Updated:Nov 29, 2025

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 configure the x-oss-forbid-overwrite request header to prevent existing objects from being overwritten by objects with the same names when you copy objects or perform simple upload or multipart upload.

Simple upload

The following sample code provides an example on how to prevent existing objects from being overwritten by objects that have the same names when you perform simple upload:

const OSS = require("ali-oss");

const client = new OSS({
  // Set region to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set region to oss-cn-hangzhou.
  region: 'yourregion',
  // Obtain access credentials from environment variables. Before running the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  authorizationV4: true,
  // Specify the bucket name.
  bucket: 'examplebucket'
});

// Set yourLocalFile to the full path of the local file.
const file = "yourLocalFile";
// Specify whether to overwrite an existing object that has the same name. In this example, this parameter is set to true to prevent overwriting. If an object with the same name exists, the program reports an error.
const headers = {
  "x-oss-forbid-overwrite": true,
};
async function put() {
  try {
    // Set yourObjectName to the full path of the object.
    const result = await client.put("yourObjectName", file, { headers });
    console.log(result);
  } catch (e) {
    console.log(e);
  }
}

put();

Copy an object

Copy a small object

The following sample code provides an example on how to copy a small object without overwriting the existing object that has the same name:

const OSS = require("ali-oss");

const client = new OSS({
  // Set region to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set region to oss-cn-hangzhou.
  region: 'yourregion',
  // Obtain access credentials from environment variables. Before running the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  authorizationV4: true,
  // Specify the bucket name.
  bucket: 'examplebucket'
});

// Specify whether to overwrite an existing object that has the same name. In this example, this parameter is set to true to prevent overwriting. If an object with the same name exists, the program reports an error.
const headers = {
  "x-oss-forbid-overwrite": true,
};
// Set yourTargetObject to the full path of the destination object.
// Set yourSourceObject to the full path of the source object.
client  
  .copy("yourTargetObject", "yourSourceObject", { headers })
  .then((res) => {
    console.log(res.res.data.toString("utf8"));
    console.log(res);
  })
  .catch((e) => {
    console.log(e);
  });

Copy a large object

The following sample code provides an example on how to prevent an existing object from being overwritten by a large object with the same name when you copy the large object by using multipart copy:

const OSS = require("ali-oss");

const client = new OSS({
  // Set yourRegion to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set region to oss-cn-hangzhou.
  region: "yourRegion",
  // Obtain access credentials from environment variables. Before running the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  authorizationV4: true,
  // Set yourTargetBucket to the name of the bucket where the destination object is located.
  bucket: "yourTargetBucket"
});

async function put() {
  try {
    const result = await client.multipartUploadCopy(
      // Specify the full path of the destination object.
      "yourTargetObject",
      {
        // Specify the full path of the source object to copy.
        sourceKey: "yourSourceObject",
        // Specify the name of the bucket where the source object is located.
        sourceBucketName: "yourSourceBucket",
      },
      {
        // Specify whether to overwrite an existing object that has the same name. In this example, this parameter is set to true to prevent overwriting. If an object with the same name exists, the program reports an error.
        headers: { "x-oss-forbid-overwrite": true },
      }
    );
    console.log(result);
  } catch (e) {
    console.log(e);
  }
}

put();

Multipart upload

The following sample code provides an example on how to prevent an existing object from being overwritten by an object with the same name when you use multipart upload to upload the object:

const OSS = require("ali-oss");

const client = new OSS({
  // Set yourRegion to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set region to oss-cn-hangzhou.
  region: "yourRegion",
  // Obtain access credentials from environment variables. Before running the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  authorizationV4: true,
  // Set yourBucketName to the name of the bucket.
  bucket: "yourBucketName",
});

// Set yourLocalFile to the full path of the local file.
const file = "yourLocalFile";
// Specify whether to overwrite an existing object that has the same name. In this example, this parameter is set to true to prevent overwriting. If an object with the same name exists, the program reports an error.
const headers = {
  "x-oss-forbid-overwrite": true,
};
{
  // Perform a multipart upload. Set yourObjectName to the full path of the object.
  client    
    .multipartUpload("yourObjectName", file, { headers })
    .then((res) => {
      console.log(res);
    })
    .catch((e) => {
      console.log(e);
    });
}

References