Updated on 2026-11-17 GMT+08:00

Restoring Archive Objects in Batches (SDK for C)

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
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include "eSDKOBS.h"
#define MAX_KEY_LEN     1024
#define MAX_TASK        10000
#define RESTORE_WAIT_SECONDS 6
// Single object restoration task
typedef struct {
    char key[MAX_KEY_LEN];
    obs_status status;  
} restore_task;
//Batch restoration result statistics
typedef struct {
    int total;
    int success;
    int failed;
    char failed_keys[100][MAX_KEY_LEN];
    int failed_count;
    pthread_mutex_t mutex;
} batch_result;
static batch_result g_result = {0, 0, 0, {{0}}, 0, PTHREAD_MUTEX_INITIALIZER};
static restore_task *g_tasks = NULL;
static int g_task_count = 0;
static int g_task_index = 0;
static pthread_mutex_t g_task_mutex = PTHREAD_MUTEX_INITIALIZER;
// Response callback function
static obs_status restore_properties_callback(const obs_response_properties *properties, void *callback_data)
{
    (void)properties;
    (void)callback_data;
    return OBS_STATUS_OK;
}
// Completion callback function
static void restore_complete_callback(obs_status status,
                                      const obs_error_details *error,
                                      void *callback_data)
{
    (void)error;
    restore_task *task = (restore_task *)callback_data;
    char *key = task ? task->key : "unknown";
    if (task) {                                                                                                                                                                    
        task->status = status;                                                                                                                                                     
    } 
    pthread_mutex_lock(&g_result.mutex);
    if (status == OBS_STATUS_OK) {
        g_result.success++;
        printf("[SUCCESS] restore %s\n", key);
    } else {
        g_result.failed++;
        if (g_result.failed_count < 100) {
            strncpy(g_result.failed_keys[g_result.failed_count], key, MAX_KEY_LEN - 1);
            g_result.failed_keys[g_result.failed_count][MAX_KEY_LEN - 1] = '\0';
            g_result.failed_count++;
        }
        printf("[FAILED] restore %s: %s\n", key, obs_get_status_name(status));
    }
    pthread_mutex_unlock(&g_result.mutex);
}
// Callback data for listing objects
typedef struct {
    restore_task *tasks;
    int max_count;
    int current_count;
    obs_status ret_status;
} list_cb_data;
// Data callback for listing objects
static obs_status list_objects_callback(int is_truncated, const char *next_marker,
                                        int contents_count,
                                        const obs_list_objects_content *contents,
                                        int common_prefixes_count,
                                        const char **common_prefixes,
                                        void *callback_data)
{
    (void)is_truncated;
    (void)next_marker;
    (void)common_prefixes_count;
    (void)common_prefixes;
    list_cb_data *data = (list_cb_data *)callback_data;
    for (int i = 0; i < contents_count && data->current_count < data->max_count; i++) {
        memset(&data->tasks[data->current_count], 0, sizeof(restore_task));
        strncpy(data->tasks[data->current_count].key, contents[i].key, MAX_KEY_LEN - 1);
        data->tasks[data->current_count].key[MAX_KEY_LEN - 1] = '\0';
        data->current_count++;
    }
    return OBS_STATUS_OK;
}
// Object listing completion callback
static void list_complete_callback(obs_status status,
                                   const obs_error_details *error,
                                   void *callback_data)
{
    (void)error;
    list_cb_data *data = (list_cb_data *)callback_data;
    data->ret_status = status;
}
// Data structure for restoration status polling
typedef struct {
    char restore_info[128];
    obs_status status;
} restore_status_data;
static obs_status restore_check_properties_callback(const obs_response_properties *properties, void *callback_data)
{
    restore_status_data *data = (restore_status_data *)callback_data;
    if (data && properties && properties->restore) {
        strncpy(data->restore_info, properties->restore, sizeof(data->restore_info) - 1);
        data->restore_info[sizeof(data->restore_info) - 1] = '\0';
    }
    return OBS_STATUS_OK;
}
static void restore_check_complete_callback(obs_status status,
                                            const obs_error_details *error,
                                            void *callback_data)
{
    (void)error;
    restore_status_data *data = (restore_status_data *)callback_data;
    if (data) {
        data->status = status;
    }
}
// Wait until the restoration of a single object is complete. (The restore header in the object metadata is polled.)
static void wait_until_restore_complete(const obs_options *options, obs_object_info *object_info)
{
    // Restoration completion marker: The restore header starts with ongoing-request="false"
    const char *prefix = "ongoing-request=\"false\"";
    obs_response_handler handler = {
        restore_check_properties_callback,
        restore_check_complete_callback
    };
    for (;;) {
        restore_status_data data;
        memset(&data, 0, sizeof(data));
        data.status = OBS_STATUS_BUTT;
        get_object_metadata(options, object_info, NULL, &handler, &data);
        if (data.status != OBS_STATUS_OK) {
            printf("[WARN] get metadata for restore check failed(%s)\n", obs_get_status_name(data.status));
            break;
        }
        if (data.restore_info[0] != '\0') {
            // Exit after the restoration is complete.
            if (strstr(data.restore_info, prefix) != NULL) {
                printf("[INFO] %s restore completed: %s\n", object_info->key, data.restore_info);
                break;
            }
        }
        // Not completed. Wait for a while and continue polling.
        sleep(RESTORE_WAIT_SECONDS);
    }
}
// Worker thread: Claim a task. Restore objects. Then, wait until the restoration is complete.
static void *worker_thread(void *arg)
{
    obs_options *options_ptr = (obs_options *)arg;
    const char *stored_days = "1";
    obs_tier tier = OBS_TIER_EXPEDITED;
    while (1) {
        pthread_mutex_lock(&g_task_mutex);
        if (g_task_index >= g_task_count) {
            pthread_mutex_unlock(&g_task_mutex);
            break;
        }
        int idx = g_task_index++;
        pthread_mutex_unlock(&g_task_mutex);
        restore_task *task = &g_tasks[idx];
        // Construct object information.
        obs_object_info object_info;
        memset(&object_info, 0, sizeof(obs_object_info));
        object_info.key = task->key;
        object_info.version_id = NULL;
        // Set the response callback.
        obs_response_handler handler = {
            restore_properties_callback,
            restore_complete_callback
        };
        // Submit a restoration request.
        task->status = OBS_STATUS_BUTT;
        restore_object(options_ptr, &object_info, stored_days, tier, &handler, task);
        if (task->status != OBS_STATUS_OK) {                                                                                                                                      
            // The restore_complete_callback has already recorded the failure. Skip waiting.
            continue;
        }
        // Wait until objects are restored.
        wait_until_restore_complete(options_ptr, &object_info);
    }
    return NULL;
}
int main()
{
    // This example shows how to restore Archive or Deep Archive objects in batches. Objects in the Archive or Deep Archive storage class are frozen and cannot be directly accessed.
    // Submit a restoration request first. The objects must be restored before they can be accessed.
    // Call the obs_initialize method at the program entry to initialize global resources such as the network and memory.
    const char *BUCKET_NAME = "example-bucket-name";
    const char *PREFIX = "test/source/";
    int THREAD_NUM = 8;
    obs_status status = obs_initialize(OBS_INIT_ALL);
    if (status != OBS_STATUS_OK) {
        printf("obs_initialize failed: %s\n", obs_get_status_name(status));
        return 1;
    }
    obs_options options;
    init_obs_options(&options);
    
    
    // Enter the endpoint corresponding to the bucket for host_name. CN-Hong Kong is used here as an example. Replace it with the one currently in use.
    options.bucket_options.host_name = "obs.ap-southeast-1.myhuaweicloud.com";
   
   
  
   
    // Hard-coded or plaintext AK and SK are risky. For security purposes, encrypt your AK and SK and store them in the configuration file or environment variables.
    // In this example, the AK and SK are stored in environment variables for identity authentication. Before running the code in this example, configure local environment variables ACCESS_KEY_ID and SECRET_ACCESS_KEY.
    options.bucket_options.access_key = getenv("ACCESS_KEY_ID");
    options.bucket_options.secret_access_key = getenv("SECRET_ACCESS_KEY");
    options.bucket_options.bucket_name = (char *)BUCKET_NAME;
    options.request_options.auth_switch = OBS_OBS_TYPE;
    printf("\nListing objects with prefix '%s'...\n", PREFIX);
    // List the objects to be restored by specifying a prefix.
    g_tasks = (restore_task *)malloc(sizeof(restore_task) * MAX_TASK);
    if (!g_tasks) {
        printf("Failed to allocate memory\n");
        obs_deinitialize();
        return 1;
    }
    list_cb_data cb_data = {g_tasks, MAX_TASK, 0, OBS_STATUS_BUTT};
    obs_list_objects_handler list_handler = {
        {NULL, list_complete_callback},
        list_objects_callback
    };
    list_bucket_objects(&options, PREFIX, NULL, NULL, 1000, &list_handler, &cb_data);
    if (cb_data.ret_status != OBS_STATUS_OK) {
        printf("list_bucket_objects failed: %s\n", obs_get_status_name(cb_data.ret_status));
        free(g_tasks);
        obs_deinitialize();
        return 1;
    }
    g_task_count = cb_data.current_count;
    if (g_task_count == 0) {
        printf("No objects found with prefix '%s'\n", PREFIX);
        free(g_tasks);
        obs_deinitialize();
        return 0;
    }
    printf("Found %d objects to restore\n", g_task_count);
    g_result.total = g_task_count;
    g_result.success = 0;
    g_result.failed = 0;
    g_result.failed_count = 0;
    g_task_index = 0;
    // Submit restoration requests in batches using multiple threads and wait until the restoration is complete.
    pthread_t threads[THREAD_NUM];
    for (int i = 0; i < THREAD_NUM; i++) {
        pthread_create(&threads[i], NULL, worker_thread, &options);
    }
    for (int i = 0; i < THREAD_NUM; i++) {
        pthread_join(threads[i], NULL);
    }
    // Output the batch restoration statistics.
    printf("\n========== Batch Restore Summary ==========\n");
    printf("Total: %d\n", g_result.total);
    printf("Success: %d\n", g_result.success);
    printf("Failed: %d\n", g_result.failed);
    if (g_result.failed_count > 0) {
        printf("\nFailed objects:\n");
        for (int i = 0; i < g_result.failed_count; i++) {
            printf("  - %s\n", g_result.failed_keys[i]);
        }
    }
    printf("============================================\n");
    free(g_tasks);
    obs_deinitialize();
    return 0;
}