This topic describes how to copy an object within a bucket or across buckets in the same region.

Usage notes

  • When you use packaging tools such as Webpack and Browserify, install OSS SDK for Browser.js by running the npm install ali-oss command.
  • OSS SDK for Browser.js is usually used in browsers. To prevent your AccessKey pair from being exposed, we recommend that you use a temporary access credential generated by Security Token Service (STS) to access OSS.

    For more information about how to set up STS, see Use a temporary access credential provided by STS to access OSS in OSS Developer Guide. You can call the AssumeRole operation or use STS SDKs for various programming languages to obtain a temporary access credential. For more information, see STS SDK overview. A temporary access credential contains a security token and a temporary AccessKey pair that consists of an AccessKey ID and an AccessKey secret.

Copy an object within a bucket

The following code provides an example on how to copy an object within the same bucket:

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <title>Document</title>
</head>

<body>
  <button id='upload'>Upload An Object</button>
  <button id='copy'>Copy An Object</button>
    <!-- import an SDK file -->
  <script type="text/javascript" src="https://gosspublic.alicdn.com/aliyun-oss-sdk-6.16.0.min.js"></script>
  <script type="text/javascript">
    const client = new OSS({
       // Set yourRegion to the endpoint of the region in which the bucket is located. For example, if your bucket is located in the China (Hangzhou) region, set yourRegion to oss-cn-hangzhou. 
       region: 'yourRegion',
       // Specify the temporary AccessKey pair obtained from STS. 
       accessKeyId: 'yourAccessKeyId',
       accessKeySecret: 'yourAccessKeySecret',
       // Specify the security token obtained from STS. 
       stsToken: 'yourSecurityToken',
       // Specify the name of the bucket. Example: examplebucket. 
       bucket: "examplebucket",
     });

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

    // Specify the object to upload. 
    const file = new Blob(['examplecontent'])
    // Specify the full path of the object. Example: exampledir/exampleobject.txt. 
    const fileName = 'exampledir/exampleobject.txt'

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

    // Copy the object. 
    copy.addEventListener('click', () => {
      // Specify the name of the destination object. 
      client.copy('newexampleobject.txt', fileName
        // Set HTTP headers and metadata for the destination object. 
        //{
            // Specify the headers parameter to set HTTP headers for the destination object. If the headers parameter is not specified, the HTTP headers of the destination object are the same as those of the source object. The HTTP headers of the source object are copied. 
            // headers:{
            //'Cache-Control': 'no-cache',        
            // If the ETag value of the source object is the same as the ETag value specified in the request, OSS copies the object and returns 200 OK. 
            //'if-match': '5B3C1A2E053D763E1B002CC607C5****',
            // If the ETag value of the source object is different from the ETag value specified in the request, OSS copies the object and returns 200 OK. 
            //'if-none-match': '5B3C1A2E053D763E1B002CC607C5****', 
            // If the specified time is earlier than the modified time of the source object, OSS copies the object and returns 200 OK. 
            //'if-modified-since': '2021-12-09T07:01:56.000Z', 
            // If the specified time is later than the modified time of the source object, OSS copies the object and returns 200 OK. 
            //'if-unmodified-since': '2021-12-09T07:01:56.000Z', 
            // Set the access control list (ACL) of the destination object to private. This indicates that only the object owner and authorized users can read and write the object. Other users have no permissions on the object. 
            //'x-oss-object-acl': 'private', 
            // Add tags to the destination object. You can add multiple tags to the object at the same time. 
            //'x-oss-tagging': 'Tag1=1&Tag2=2',
            // Specifies whether the CopyObject operation overwrites the object of the same name. Set x-oss-forbid-overwrite to true, which indicates that the object with the same name in the bucket cannot be overwritten. 
            //'x-oss-forbid-overwrite': 'true', 
    //},
        // Specify the meta parameter to set the metadata for the destination object. If the meta parameter is not specified, the metadata of the object is the same as that of the source object. The metadata of the source object is copied. 
        // meta:{
            // location: 'hangzhou',
            // year: 2015,
            // people: 'mary'
        //}
        // }
      ).then(r => {
        console.log(r.res.status)
      })
    })

  </script>
</body>

</html>

Copy an object across buckets

The following code provides an example on how to copy the srcobject.txt object from the source bucket named srcbucket to the destobject.txt object in the destination bucket named destbucket :

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Document</title>
  </head>

  <body>
    <button id="copy">Copy An Object</button>
    <!-- import an SDK file -->
    <script
      type="text/javascript"
      src="https://gosspublic.alicdn.com/aliyun-oss-sdk-6.16.0.min.js"
    ></script>
    <script type="text/javascript">      
      const client = new OSS({
         // Set yourRegion to the endpoint of the region in which the bucket is located. For example, if your bucket is located in the China (Hangzhou) region, set yourRegion to oss-cn-hangzhou. 
         region: 'yourRegion',
         // Specify the temporary AccessKey pair obtained from STS. 
         accessKeyId: 'yourAccessKeyId',
         accessKeySecret: 'yourAccessKeySecret',
         // Specify the security token obtained from STS. 
         stsToken: 'yourSecurityToken',
     // Specify the name of the destination bucket. 
         bucket: "destbucket",
      });
      const copy = document.getElementById("copy");

      const fileContent = Array(1024 * 1024 * 5)
        .fill("a")
        .join("");
      const file = new File([fileContent], fileName);

      copy.addEventListener("click", () => {
        client
          .copy(
            // Specify the name of the destination object. 
            "destobject.txt", 
            // Specify the name of the source object. 
            "srcobject.txt", 
            // Specify the name of the source bucket. 
            "sourcebucket" 
          )
          .then((r) => console.log(r));
      });
    </script>
  </body>
</html>

References

  • For more information about the complete sample code that is used to copy an object, visit GitHub.
  • For more information about the API operation that is called to copy an object, see CopyObject.