All Products
Search
Document Center

Object Storage Service:Convert the storage classes of objects

Last Updated:Nov 18, 2024

Object Storage Service (OSS) provides the following storage classes to cover various data storage scenarios from hot data to cold data: Standard, Infrequent Access (IA), Archive, Cold Archive, and Deep Cold Archive. In OSS, once an object is created, its content cannot be modified. If you want to convert the storage class of an object, you must use the Bucket.CopyObject method to copy the object to create a new object and convert the storage class of the new object.

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.

  • If you want to access an OSS bucket from a browser but no CORS rules are configured for the bucket, the browser rejects the request. Therefore, you must configure CORS rules for a bucket if you want to access the bucket from a browser. For more information, see Installation.

  • In most cases, OSS SDK for Browser.js is used in browsers. To prevent your AccessKey pair from being exposed, we recommend that you use temporary access credentials obtained from Security Token Service (STS) to access OSS.

    The temporary access credentials consist of an AccessKey pair and a security token. The AccessKey pair consists of an AccessKey ID and an AccessKey secret. For more information about how to obtain temporary access credentials, see Use STS for temporary access authorization.

Sample code

Convert the storage class of an object from Standard or IA to Archive

<!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'>Convert Storage Class</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 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',
       authorizationV4: true,
       // Specify the temporary AccessKey pair obtained from Security Token Service (STS). The AccessKey pair consists of an AccessKey ID and an AccessKey secret. 
       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 content of the object to upload. 
    const file = new Blob(['examplecontent'])
    // Specify the full path of the object to upload. Example: exampledir/exampleobject.txt. 
    const fileName = 'exampledir/exampleobject.txt'

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

    // Convert the storage class of the object by copying the object. 
    copy.addEventListener('click', () => {
      // Specify the name of the destination object. Example: newexampleobject.txt. 
      client.copy('newexampleobject.txt', fileName, {
        headers: {
          // Specify the storage class of the destination object as Archive. If you want to convert the storage class of the object to Cold Archive, replace Archive with Cold Archive. 
          'x-oss-storage-class': 'Archive'
        }
      }
      ).then(r => {
        console.log(r.res.status)
      })
    })

  </script>
</body>

</html>

Convert the storage class of an object from Archive to IA or Standard

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

  <body>
    <button id="check">Check Storage Class</button>
    <button id="restore">Restore an Object</button>
    <button id="change">Convert Storage Class</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 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',
        // Specify the temporary AccessKey pair obtained from STS. The AccessKey pair consists of an AccessKey ID and an AccessKey secret. 
        accessKeyId: 'yourAccessKeyId',
        accessKeySecret: 'yourAccessKeySecret',
        // Specify the security token obtained from STS. 
        stsToken: 'yourSecurityToken',
        // Specify the name of the bucket. Example: examplebucket. 
        bucket: "examplebucket",
     });
 
      const check = document.getElementById("check");
      const change = document.getElementById("change");
      const restore = document.getElementById("restore");

      check.addEventListener("click", () => {
        console.log ("Query the storage class of the object")
        // Check the header of the object in the developer tools of your browser to query the storage class of the object. 
        client.head("srcobject.txt").then((r) => console.log(r));
      });
      // Restore the object. 
      restore.addEventListener("click", () => {
        // Specify the name of the object to be restored. Example: srcobject.txt. 
        client.restore("srcobject.txt").then((r) => {
          console.log(r);
          console.log ("Start to restore the object")
        });
      });

      // Start to convert the storage class of the object after the object is restored. 
      change.addEventListener("click", () => {
        // The duration of the restoration is determined by the object size. 
        console.log ("Start to convert the storage class of the object")
        client
          // Specify destobject.txt as the object name after srcobject.txt is copied. 
          .copy("destobject.txt", "srcobject.txt", {
            // Specify that the storage class of the object is converted to IA. If you want to convert the storage class of the object to Standard, replace IA with Standard. 
            headers: { "x-oss-storage-class": "IA" },
          })
          .then((r) => console.log(r))
          // A common reason why an error is reported when you convert the storage class is that the object is still being restored. 
          .catch((e) => console.log ("conversion error:", e))
      });
    </script>
  </body>
</html>
                    

References

  • For the complete sample code that is used to convert the storage class of an object, visit GitHub.

  • For more information about the API operation that you can call to convert the storage class of an object, see CopyObject.