All Products
Search
Document Center

Object Storage Service:copy an object (browser.js sdk)

Last Updated:Mar 20, 2026

Copy an object within a bucket or across buckets in the same region using OSS SDK for Browser.js.

Prerequisites

Before you begin, make sure that you have:

  • Installed OSS SDK for Browser.js. If you use a packaging tool such as Webpack or Browserify, run npm install ali-oss to install the SDK.

  • Configured cross-origin resource sharing (CORS) rules on both the source and destination buckets. For details, see Preparations.

  • Obtained temporary access credentials from Security Token Service (STS): an AccessKey ID, an AccessKey secret, and a security token. Using STS credentials prevents exposing your long-term AccessKey pair in browser-side code. For details, see Use STS for temporary access authorization.

Permissions

By default, an Alibaba Cloud account has full permissions. RAM users and RAM roles have no permissions by default and must be granted the required actions through RAM Policy or bucket policies.

APIActionWhen required
CopyObjectoss:GetObjectAlways required
CopyObjectoss:PutObjectAlways required
CopyObjectoss:GetObjectVersionRequired when specifying the source object version via versionId
CopyObjectoss:GetObjectTaggingRequired when copying object tags via x-oss-tagging
CopyObjectoss:PutObjectTaggingRequired when copying object tags via x-oss-tagging
CopyObjectoss:GetObjectVersionTaggingRequired when specifying tags of a specific source object version via versionId
CopyObjectkms:GenerateDataKeyRequired when the destination object metadata contains X-Oss-Server-Side-Encryption: KMS
CopyObjectkms:DecryptRequired when the destination object metadata contains X-Oss-Server-Side-Encryption: KMS

Copy an object within a bucket

The following example copies srcobject.txt to destobject.txt within examplebucket. Initialize the OSS client with STS credentials, then call client.copy() with the destination and source object names.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <title>Document</title>
</head>
<body>
  <button id="upload">Upload</button>
  <button id="copy">Copy</button>
  <!-- Import the SDK file. -->
  <script type="text/javascript" src="https://gosspublic.alicdn.com/aliyun-oss-sdk-6.18.0.min.js"></script>
  <script type="text/javascript">
    const client = new OSS({
      // Specify the region where the bucket is located.
      // Example: if the bucket is in China (Hangzhou), set region to oss-cn-hangzhou.
      region: 'yourRegion',
      authorizationV4: true,
      // STS temporary credentials.
      accessKeyId: 'yourAccessKeyId',
      accessKeySecret: 'yourAccessKeySecret',
      stsToken: 'yourSecurityToken',
      bucket: 'examplebucket',
    });

    const upload = document.getElementById('upload');
    const copy = document.getElementById('copy');

    // Content and name of the object to upload.
    const file = new Blob(['examplecontent']);
    const fileName = 'srcobject.txt';

    // Upload the source object.
    upload.addEventListener('click', () => {
      client.put(fileName, file).then(r => console.log(r));
    });

    // Copy the object within the same bucket.
    copy.addEventListener('click', () => {
      client.copy('destobject.txt', fileName)
        .then(r => console.log(r.res.status));
    });
  </script>
</body>
</html>

Replace the following placeholders before running the example:

PlaceholderDescriptionExample
yourRegionRegion where the bucket is locatedoss-cn-hangzhou
yourAccessKeyIdTemporary AccessKey ID from STSSTS.NUgYrLnoC55...
yourAccessKeySecretTemporary AccessKey secret from STSxXxXxXx...
yourSecurityTokenSecurity token from STSCAISxgF1q6Ft5B2yfSjI...

A successful copy returns HTTP status 200.

Set HTTP headers and object metadata (optional)

Pass a third argument to client.copy() to customize the destination object. If omitted, the destination object inherits the HTTP headers and metadata of the source object.

client.copy('destobject.txt', fileName, {
  headers: {
    // Set the Cache-Control header for the destination object.
    'Cache-Control': 'no-cache',
    // Conditional copy: copy only if the source ETag matches.
    'if-match': '5B3C1A2E053D763E1B002CC607C5****',
    // Conditional copy: copy only if the source ETag does not match.
    'if-none-match': '5B3C1A2E053D763E1B002CC607C5****',
    // Conditional copy: copy only if the source object was modified after this time.
    'if-modified-since': '2021-12-09T07:01:56.000Z',
    // Conditional copy: copy only if the source object was not modified after this time.
    'if-unmodified-since': '2021-12-09T07:01:56.000Z',
    // Set the access control list (ACL) for the destination object.
    // private: only the object owner and authorized users have read and write access.
    'x-oss-object-acl': 'private',
    // Attach tags to the destination object. Separate multiple tags with ampersands.
    'x-oss-tagging': 'Tag1=1&Tag2=2',
    // Prevent overwriting a destination object with the same name.
    'x-oss-forbid-overwrite': 'true',
  },
  meta: {
    // Custom metadata for the destination object.
    location: 'hangzhou',
    year: 2015,
    people: 'mary',
  },
});

Copy an object between buckets

The following example copies srcobject.txt from srcbucket to destobject.txt in destbucket. Both buckets must be in the same region. Initialize the client with the destination bucket, then pass the source bucket name as the third argument to client.copy().

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <title>Document</title>
</head>
<body>
  <button id="copy">Copy</button>
  <!-- Import the SDK file. -->
  <script
    type="text/javascript"
    src="https://gosspublic.alicdn.com/aliyun-oss-sdk-6.18.0.min.js"
  ></script>
  <script type="text/javascript">
    const client = new OSS({
      region: 'yourRegion',
      authorizationV4: true,
      accessKeyId: 'yourAccessKeyId',
      accessKeySecret: 'yourAccessKeySecret',
      stsToken: 'yourSecurityToken',
      // Specify the destination bucket.
      bucket: 'destbucket',
    });

    const copy = document.getElementById('copy');

    copy.addEventListener('click', () => {
      client
        .copy(
          'srcobject.txt',   // Destination object name.
          'destobject.txt',  // Source object name.
          'srcbucket'        // Source bucket name.
        )
        .then(r => console.log(r));
    });
  </script>
</body>
</html>

A successful copy returns HTTP status 200.

What's next