Archived objects must be restored before they can be read. This page shows how to use the OSS C SDK to restore an archived object.
Prerequisites
Before you begin, ensure that you have:
An OSS bucket with at least one archived object
The
oss:RestoreObjectpermission. For details, see Attach a custom policy to a RAM userAn initialized OSSClient instance. If you want to initialize OSSClient using custom domain names or Security Token Service (STS), see Initialization
Usage notes
RestoreObjectapplies only to archived objects.The examples on this page use the public endpoint for the China (Hangzhou) region. To access OSS from another Alibaba Cloud service in the same region, use an internal endpoint instead. For a full list of regions and endpoints, see Regions and endpoints.
Sample code
The following example submits a restore request and polls until the server returns a status code other than 409. HTTP 409 indicates that a restore operation is already in progress.
Note: Restoration is asynchronous and may take time to complete. For production workloads, use a longer polling interval or use OSS event notifications to detect when the restore is complete, rather than polling in a tight loop.
#include "oss_api.h"
#include "aos_http_io.h"
/* Set the endpoint for the region where your bucket is located.
Example: https://oss-cn-hangzhou.aliyuncs.com */
const char *endpoint = "yourEndpoint";
/* Bucket name. Example: examplebucket */
const char *bucket_name = "examplebucket";
/* Full object path, excluding the bucket name.
Example: exampledir/exampleobject.txt */
const char *object_name = "exampledir/exampleobject.txt";
/* Region ID for the bucket. Example: cn-hangzhou */
const char *region = "yourRegion";
void init_options(oss_request_options_t *options)
{
options->config = oss_config_create(options->pool);
/* Initialize aos_string_t fields from char* strings. */
aos_str_set(&options->config->endpoint, endpoint);
/* Load access credentials from environment variables.
Set OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET before running this code. */
aos_str_set(&options->config->access_key_id, getenv("OSS_ACCESS_KEY_ID"));
aos_str_set(&options->config->access_key_secret, getenv("OSS_ACCESS_KEY_SECRET"));
aos_str_set(&options->config->region, region);
options->config->signature_version = 4;
/* Set is_cname to 0 if you are not using a custom domain name. */
options->config->is_cname = 0;
/* Configure network settings such as timeout. */
options->ctl = aos_http_controller_create(options->pool, 0);
}
int main(int argc, char *argv[])
{
/* Initialize global resources (network, memory) at program entry. */
if (aos_http_io_initialize(NULL, 0) != AOSE_OK) {
exit(1);
}
/* Create a memory pool for this request. */
aos_pool_t *pool;
aos_pool_create(&pool, NULL);
/* Create and initialize the OSS client options. */
oss_request_options_t *oss_client_options;
oss_client_options = oss_request_options_create(pool);
init_options(oss_client_options);
/* Initialize request parameters. */
aos_string_t bucket;
aos_string_t object;
aos_table_t *headers = NULL;
aos_table_t *resp_headers = NULL;
aos_status_t *resp_status = NULL;
aos_str_set(&bucket, bucket_name);
aos_str_set(&object, object_name);
/* Submit the restore request. Retry while HTTP 409 indicates a restore is already in progress. */
do {
headers = aos_table_make(pool, 0);
resp_status = oss_restore_object(oss_client_options, &bucket, &object, headers, &resp_headers);
printf("restore object resp_status->code: %d \n", resp_status->code);
if (resp_status->code != 409) {
break;
} else {
printf("restore object is already in progress, resp_status->code: %d \n", resp_status->code);
apr_sleep(5000);
}
} while (1);
/* Release the memory pool and free all resources allocated during the request. */
aos_pool_destroy(pool);
/* Release global resources. */
aos_http_io_deinitialize();
return 0;
}Replace the following placeholders before running the code:
| Placeholder | Description | Example |
|---|---|---|
yourEndpoint | Endpoint for the region where your bucket is located | https://oss-cn-hangzhou.aliyuncs.com |
yourRegion | Region ID for your bucket | cn-hangzhou |
examplebucket | Your bucket name | examplebucket |
exampledir/exampleobject.txt | Full path of the object to restore | mydir/archive.zip |