All Products
Search
Document Center

Object Storage Service:Restore objects (C# SDK V1)

Last Updated:Nov 28, 2025

You must restore Archive or Cold Archive objects before you can read them. This topic describes how to restore these objects.

Usage notes

  • You can call the RestoreObject method only for Archive and Cold Archive objects.

  • In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about OSS regions and endpoints, see Regions and endpoints.

  • In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Initialization.

  • To restore objects, you must have the oss:RestoreObject permission. For more information, see Attach a custom policy to a RAM user.

Restore an Archive object

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

using Aliyun.OSS;
using Aliyun.OSS.Model;
using Aliyun.OSS.Model;
using System.Net;
using System.Text;
using Aliyun.OSS.Common;

// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
var endpoint = "yourEndpoint";
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
// Specify the bucket name.
var bucketName = "examplebucket";
// Specify the full path of the Archive object. The path cannot contain the bucket name.
var objectName = "yourObjectName";
// Specify the object content.
var objectContent = "More than just cloud.";
int maxWaitTimeInSeconds = 600;
// 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 cn-hangzhou.
const string region = "cn-hangzhou";

// Create a ClientConfiguration instance and modify the default parameters as needed.
var conf = new ClientConfiguration();

// Set the signature version to V4.
conf.SignatureVersion = SignatureVersion.V4;

// Create an OssClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);
client.SetRegion(region);
try
{
    // Create an Archive bucket.
    var bucket = client.CreateBucket(bucketName, StorageClass.Archive);
    Console.WriteLine("Create Archive bucket succeeded, {0} ", bucket.Name);
}
catch (Exception ex)
{
    Console.WriteLine("Create Archive bucket failed, {0}", ex.Message);
}
// Upload the object and set the storage class to Archive.
try
{
    byte[] binaryData = Encoding.ASCII.GetBytes(objectContent);
    MemoryStream requestContent = new MemoryStream(binaryData);
    client.PutObject(bucketName, objectName, requestContent);
    Console.WriteLine("Put object succeeded, {0}", objectName);
}
catch (Exception ex)
{
    Console.WriteLine("Put object failed, {0}", ex.Message);
}
var metadata = client.GetObjectMetadata(bucketName, objectName);
string storageClass = metadata.HttpMetadata["x-oss-storage-class"] as string;
if (storageClass != "Archive")
{
    Console.WriteLine("StorageClass is {0}", storageClass);
    return;
}
// Restore the Archive object.
RestoreObjectResult result = client.RestoreObject(bucketName, objectName);
Console.WriteLine("RestoreObject result HttpStatusCode : {0}", result.HttpStatusCode);
if (result.HttpStatusCode != HttpStatusCode.Accepted)
{
    throw new OssException(result.RequestId + ", " + result.HttpStatusCode + " ,");
}
while (maxWaitTimeInSeconds > 0)
{
    var meta = client.GetObjectMetadata(bucketName, objectName);
    string restoreStatus = meta.HttpMetadata["x-oss-restore"] as string;
    if (restoreStatus != null && restoreStatus.StartsWith("ongoing-request=\"false\"", StringComparison.InvariantCultureIgnoreCase))
    {
        break;
    }
    Thread.Sleep(1000);
    // Decrease the maximum wait time by 1 second.
    maxWaitTimeInSeconds--;
}
if (maxWaitTimeInSeconds == 0)
{
    Console.WriteLine("RestoreObject is timeout. ");
    throw new TimeoutException();
}
else
{
    Console.WriteLine("RestoreObject is successful. ");
}

Restore a Cold Archive object

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

using Aliyun.OSS;
using Aliyun.OSS.Model;
using Aliyun.OSS.Common;

// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
var endpoint = "yourEndpoint";
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
// Specify the bucket name.
var bucketName = "examplebucket";
// Specify the full path of the Cold Archive object. The path cannot contain the bucket name.
var objectName = "yourObjectName";
// 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 cn-hangzhou.
const string region = "cn-hangzhou";

// Create a ClientConfiguration instance and modify the default parameters as needed.
var conf = new ClientConfiguration();

// Set the signature version to V4.
conf.SignatureVersion = SignatureVersion.V4;

// Create an OssClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);
client.SetRegion(region);

// Restore the Cold Archive object.
var request = new RestoreObjectRequest(bucketName, objectName);

// Set the restoration priority for the Cold Archive object.
// TierType.Expedited: The object is restored within 1 hour.
// TierType.Standard: The object is restored within 2 to 5 hours.
// TierType.Bulk: The object is restored within 5 to 12 hours.
// Configure the restoration parameters. For example, set the restoration to complete within 1 hour and keep the object in the restored state for 2 days.
// The first parameter, Days, specifies the number of days that the object remains in the restored state. The default value is 1. This parameter applies to both Archive and Cold Archive objects.
// The second parameter, Tier, specifies the restoration priority. This parameter applies only to Cold Archive objects.
request.Days = 2;
request.Tier = TierType.Bulk;
RestoreObjectResult result = client.RestoreObject(request);
Console.WriteLine("RestoreObject result HttpStatusCode : {0}", result.HttpStatusCode);

References

  • For the complete sample code to restore Archive and Cold Archive objects, see GitHub example.

  • For more information about the API operation to restore Archive and Cold Archive objects, see RestoreObject.