Updated on 2026-07-22 GMT+08:00

Restoring Archive Objects in Batches (SDK for Node.js)

If you have any questions during development, post them on the Issues page of GitHub.

Sample Code

This example restores Archive objects in batches. Archive objects must be restored before they can be accessed.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Import the OBS library.
//Use npm for installation.
const ObsClient = require("esdk-obs-nodejs");
// Use the source code for installation.
// var ObsClient = require('./lib/obs');

// Create an ObsClient instance.
const obsClient = new ObsClient({
  // Obtain an AK and SK pair using environment variables (recommended) or import it in other ways. Using hard coding may result in leakage.
  // Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html.
  access_key_id: process.env.ACCESS_KEY_ID,
  secret_access_key: process.env.SECRET_ACCESS_KEY,
  // (Optional) If you use a temporary AK/SK pair and a security token to access OBS, you are not advised to use hard coding, which may result in information leakage. You can obtain an AK/SK pair using environment variables or import an AK/SK pair in other ways.
  // security_token: process.env.SECURITY_TOKEN,
  // Enter the endpoint corresponding to the bucket. CN-Hong Kong is used here as an example. Replace it with the one currently in use.
  server: "https://obs.ap-southeast-1.myhuaweicloud.com"
});

/**
 * Restoring Archive objects in batches
* Archive objects must be restored before you can access them. The restoration process usually takes several minutes to several hours.
 */
async function batchRestoreObjects() {
  try {
    const bucketName = "examplebucket";
    const prefix = "archive/";
    const params = {
      Bucket: bucketName,
      Prefix: prefix,
      EncodingType: "url",
    };
    // Storage classes of objects to be restored: Archive (COLD) and Deep Archive (DEEP_ARCHIVE)
    const restorableStorageClasses = ["COLD", "DEEP_ARCHIVE"];
    // List the objects to be restored.
    let objectsToRestore = [];
    let allListed = 0;
    while (true) {
      const listResult = await obsClient.listObjects(params);
      if (listResult.CommonMsg.Status > 300) {
        console.log("List objects failed: Status=%d, Code=%s, Message=%s",
          listResult.CommonMsg.Status, listResult.CommonMsg.Code, listResult.CommonMsg.Message);
        return;
      }
      if (listResult.InterfaceResult.Contents && listResult.InterfaceResult.Contents.length > 0) {
        // Filter out objects in the Archive and Deep Archive storage classes.
        const restorable = listResult.InterfaceResult.Contents.filter(
          (obj) => restorableStorageClasses.includes(obj.StorageClass)
        );
        objectsToRestore = objectsToRestore.concat(restorable);
        // Collect statistics on non-target objects for log display.
        const skipped = listResult.InterfaceResult.Contents.length - restorable.length;
        if (skipped > 0) {
          console.log(`Skipped ${skipped} non-archive objects in this listing (StorageClass: ${listResult.InterfaceResult.Contents[0]?.StorageClass || 'N/A'})`);
        }
        allListed += listResult.InterfaceResult.Contents.length;
      }
      if (listResult.InterfaceResult.IsTruncated === "true") {
        params.Marker = listResult.InterfaceResult.NextMarker;
      } else {
        break;
      }
    }
    console.log(`Listed ${allListed} objects, of which ${objectsToRestore.length} are restorable archive/deep-archive objects, starting batch restore...`);
    // Restore objects in batches.
    const restoreDays = 1; // Number of days to retain restored objects.
    const restoreTier = "Expedited"; // Restoration speed: Expedited, Standard, or Bulk.
    for (const obj of objectsToRestore) {
      const restoreParams = {
        Bucket: bucketName,
        Key: obj.Key,
        RestoreRequest: {
          Days: restoreDays,
          RestoreJobDefinition: {
            Tier: restoreTier,
          },
        },
      };
      const result = await obsClient.restoreObject(restoreParams);
      if (result.CommonMsg.Status <= 300) {
        console.log(``Object [${obj.Key}] restore request submitted successfully, RequestId: ${result.CommonMsg.RequestId}`);
      } else {
        console.log(`Object [${obj.Key}] restore failed: Status=${result.CommonMsg.Status}, Code=${result.CommonMsg.Code}, Message=${result.CommonMsg.Message}`);
      }
    }
    console.log(`Batch restore complete`);
  } catch (error) {
    console.log("Client exception:");
    console.log(error);
  }
}
batchRestoreObjects();

To extend the validity period of the Archive data restored, you can repeatedly restore the data, but you will be billed for each restoration. After a second restore, the validity period of Standard object copies will be prolonged, and you need to pay for storing these copies during the prolonged period.