All Products
Search
Document Center

Object Storage Service:Prevent objects from being overwritten by objects that have the same names

Last Updated:Nov 05, 2024

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({
  // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to oss-cn-hangzhou. 
  region: 'yourregion',
  // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  authorizationV4: true,
  // Specify the name of the bucket. 
  bucket: 'examplebucket'
});

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

put();

Copy objects

Disable overwrites for copying 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({
  // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to oss-cn-hangzhou. 
  region: 'yourregion',
  // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  authorizationV4: true,
  // Specify the name of the bucket. 
  bucket: 'examplebucket'
});

// Specify whether to overwrite an existing object with the same name. In this example, this parameter is set to true, which specifies that the operation does not overwrite an existing object that has the same name. If an object with the same name exists in the bucket, OSS returns an error. 
const headers = {
  "x-oss-forbid-overwrite": true,
};
// Specify the full path of the destination object. 
// Specify 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);
  });

Disable overwrites for copying 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({
  // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to oss-cn-hangzhou. 
  region: "yourRegion",
  // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  authorizationV4: true,
  // Specify the name of the destination bucket. 
  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. 
        sourceKey: "yourSourceObject",
        // Specify the name of the source bucket. 
        sourceBucketName: "yourSourceBucket",
      },
      {
        // Specify whether to overwrite the existing object with the same name. In this example, this parameter is set to true, which specifies that the operation does not overwrite an existing object that has the same name. If an object with the same name exists in the bucket, OSS returns 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({
  // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to oss-cn-hangzhou. 
  region: "yourRegion",
  // Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  authorizationV4: true,
  // Specify the name of the bucket. 
  bucket: "yourBucketName",
});

// Specify the name of the local file. 
const file = "yourLocalFile";
// Specify whether to overwrite an existing object with the same name. In this example, this parameter is set to true, which specifies that the operation does not overwrite an existing object that has the same name. If an object with the same name exists in the bucket, OSS returns an error. 
const headers = {
  "x-oss-forbid-overwrite": true,
};
{
  // Start the multipart upload task. // Specify the full path of the object. 
  client    
    .multipartUpload("yourObjectName", file, { headers })
    .then((res) => {
      console.log(res);
    })
    .catch((e) => {
      console.log(e);
    });
}

References

  • For more information about the API operation that you can call to perform simple upload, see PutObject.

  • For more information about the API operation that you can call to copy an object, see CopyObject.

  • For more information about the API operations that you can call to perform multipart upload, see InitiateMultipartUpload and CompleteMultipartUpload.