Object Storage Service (OSS) provides the following storage classes to cover a variety of data storage scenarios from hot data to cold data: Standard, Infrequent Access (IA), Archive, and Cold Archive. This topic describes how to convert the storage class of an 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.
  • 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.

    For more information about how to use STS, see Use temporary credentials 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 temporary access credentials. Temporary access credentials include a security token and a temporary AccessKey pair that consists of an AccessKey ID and an AccessKey secret.

Sample code

  • Convert the storage class of an object from Standard or Infrequent Access (IA) to Archive or Cold Archive

    The following code provides an example on how to 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 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 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 source 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 or Cold Archive to Standard or IA

    The following code provides an example of how to convert the storage class of an object from Archive or Cold Archive to Standard or IA:

    <!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 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 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("newName.txt").then((r) => console.log(r));
          });
          // Restore the object. 
          restore.addEventListener("click", () => {
            // Specify the name of the destination object. Example: newexampleobject.txt. 
            client.restore("newexampleobject.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
              .copy("newexampleobject.txt", "newName.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))
              // The 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 more information about 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 is called to convert the storage class of an object, see CopyObject.