Updated on 2026-08-14 GMT+08:00

Restoring Archive Objects in Batches

Function

You can use the APIs for listing objects and restoring Archive objects to restore objects in batches.

Sample Code

This example restores objects in batches.

// Hard-coded or plaintext AK and SK are risky. For security purposes, encrypt your AK and SK before storing them in the configuration file or environment variables. In this example, the AK and SK are stored in environment variables. Before running the code in this example, configure environment variables AccessKeyID and SecretAccessKey.
// The front-end code does not have the process environment variable, so you need to use a module bundler like webpack to define the process variable.
// 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.
const AK = process.env.AccessKeyID
const SK = process.env.SecretAccessKey
// (Optional) If you use a temporary AK and SK pair and a security token to access OBS, obtain them using environment variables.
const security_token= process.env.SecurityToken
// Set server to the endpoint corresponding to the bucket. CN-Hong Kong is used here as an example. Replace it with the one currently in use.
const server = "https://obs.ap-southeast-1.myhuaweicloud.com"

// Create an ObsClient instance.
var obsClient = new ObsClient({
    access_key_id: AK,
    secret_access_key: SK,
    server: server
});

// Bucket name
var bucket = 'examplebucket';
// Prefix of the objects to be restored. If this parameter is left blank, all objects will be restored.
var prefix = '';
// Number of days to retain restored objects
var days = 1;
// Restoration mode: Expedited or Standard
var tier = 'Expedited';
async function restoreObjects() {
    var marker = '';
    var totalCount = 0;
    console.log('Start listing and restoring objects...');
    while (true) {
        var listParams = { Bucket: bucket };
        if (prefix) {
            listParams.Prefix = prefix;
        }
        if (marker) {
            listParams.Marker = marker;
        }
        var listResult = await obsClient.listObjects(listParams);
        if (listResult.CommonMsg.Status <= 300) {
            if (listResult.InterfaceResult.Contents) {
                var batchCount = listResult.InterfaceResult.Contents.length;
                console.log ('Obtained ' + batchCount + ' objects in this batch. Starting restoration...');
                for (var i = 0; i < listResult.InterfaceResult.Contents.length; i++) {
                    var key = listResult.InterfaceResult.Contents[i].Key;
                    totalCount++;
                    try {
                        var params = {
                            Bucket: bucket,
                            Key: key,
                            Days: days,
                            Tier: tier
                        };
                        var result = await obsClient.restoreObject(params);
                        if (result.CommonMsg.Status <= 300) {
                            console.log('[' + totalCount + '] succeeded: ' + key);
                        } else {
                            console.log('[' + totalCount + '] failed: ' + key + ' - ' + result.CommonMsg.Message);
                        }
                    } catch (error) {
                        console.log('[' + totalCount + '] exception occurred: ' + key + ' - ' + error);
                    }
                }
            }
            if (listResult.InterfaceResult.IsTruncated == 'true') {
                marker = listResult.InterfaceResult.NextMarker;
            } else {
                break;
            }
        } else {
            console.log('Failed to list objects: ', listResult.CommonMsg.Message);
            return;
        }
    }
    console.log('Batch restoration completed. A total of ' + totalCount + ' objects are processed.');
}
restoreObjects()