All Products
Search
Document Center

Object Storage Service:Restore objects

Last Updated:Oct 07, 2023

You need to restore an Archive or Cold Archive object before you can read it. This topic describes how to restore an Archive or Cold Archive 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.

Restore an Archive object

The following sample code provides an example on how to restore an Archive object:

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

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

<body>
  <button id="upload">Upload an Object</button>
  <button id='restore'>Restore an Object</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 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 restore = document.getElementById('restore')

    // Specify the content of the object to upload. 
    const file = new Blob(['examplecontent'])
    // Specify the name of the object to upload to the bucket. 
    const fileName = 'example.txt'

    // Upload the object and specify its storage class as Archive. 
    upload.addEventListener('click', () => {
      client.put(fileName, file, {
        headers: {
          'x-oss-storage-class': 'Archive'
        }
      }).then(r => console.log(r))
    })
    // Restore the Archive object. 
    restore.addEventListener('click', () => {
      client.restore(fileName).then(r => console.log(r))
    })

  </script>
</body>

</html>

Restore a Cold Archive object

The following code provides an example on how to restore a Cold Archive object:

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

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

<body>
  <button id="upload">Upload an Object</button>
  <button id='restore'>Restore an Object</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 upload = document.getElementById("upload");
      const restore = document.getElementById("restore");

      // Specify the content of the object to upload. 
      const file = new Blob(["examplecontent"]);
      // Specify the name of the object to upload to the bucket. 
      const fileName = "example1.txt";

      // Upload the object and specify its storage class as Cold Archive. 
      upload.addEventListener("click", () => {
        client
          .put(fileName, file, {
            headers: {
              "x-oss-storage-class": "ColdArchive",
            },
          })
          .then((r) => console.log(r));
      });    
      // Expedited: The object is restored within 1 hour. 
      // Standard: The object is restored within 2 to 5 hours. If the JobParameters parameter is not passed in, the default restoration priority is Standard. 
      // Bulk: The object is restored within 5 to 12 hours. 
      // Days specifies the duration in which the object can remain in the restored state. Unit: days. Valid values: 1 to 7. 
      // Restore the Cold Archive object. 
      restore.addEventListener("click", () => {
        client
          .restore(fileName, {
            type: "ColdArchive",
            // Specify the restoration priority. 
            // Expedited: The file is restored within 1 hour.
            // Standard: The object is restored within 2 to 5 hours. 
            // Bulk: The object is restored within 5 to 12 hours. 
            JobParameters: "Standard",
            // Specifies the duration in which the object can remain in the restored state. Default value: 1 day. 
            Days: 2,
          })
          .then((r) => console.log(r));
      });
    </script>
  </body>
</html>

References

  • For more information about the complete sample code that is used to restore an Archive or Cold Archive object, visit GitHub.

  • For more information about the API operation that you can call to restore an Archive or Cold Archive object, see RestoreObject.