In a versioning-enabled bucket, different versions of the objects may have different storage classes. By default, the RestoreObject operation restores the current version of an object. To restore a specific version of the object, specify the version ID.
The following code provides an example on how to restore an object:
using Aliyun.OSS;
using Aliyun.OSS.Model;
var endpoint = "<yourEndpoint>";
var accessKeyId = "<yourAccessKeyId>";
var accessKeySecret = "<yourAccessKeySecret>";
var bucketName = "<yourBucketName>";
var objectName = "<yourObjectName>";
var objectContent = "More than just cloud.";
var versionid = "<yourArchiveObjectVersionid>";
int maxWaitTimeInSeconds = 600;
// Create an OSSClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
var getrequest = new GetObjectMetadataRequest(bucketName, objectName)
{
// Specify the ID of the version that is of the Archive storage class.
VersionId = versionid
};
var metadata = client.GetObjectMetadata(getrequest);
string storageClass = metadata.HttpMetadata["x-oss-storage-class"] as string;
if (storageClass ! = "Archive")
{
Console.WriteLine("StorageClass is {0}", storageClass);
return;
}
// Restore the object.
var request = new RestoreObjectRequest(bucketName, objectName)
{
// Specify the ID of the version that is of the Archive storage class.
VersionId = versionid
};
RestoreObjectResult result = client.RestoreObject(request);
Console.WriteLine("RestoreObject result HttpStatusCode : {0}, versionid: {1}", result.HttpStatusCode, result.VersionId);
if (result.HttpStatusCode ! = HttpStatusCode.Accepted)
{
throw new OssException(result.RequestId + ", " + result.HttpStatusCode + " ,");
}
while (maxWaitTimeInSeconds > 0)
{
var req = new GetObjectMetadataRequest(bucketName, objectName)
{
// Specify the ID of the version that is of the Archive storage class.
VersionId = versionid
};
var meta = client.GetObjectMetadata(req);
string restoreStatus = meta.HttpMetadata["x-oss-restore"] as string;
if (restoreStatus ! = null && restoreStatus.StartsWith("ongoing-request=\"false\"", StringComparison.InvariantCultureIgnoreCase))
{
break;
}
Thread.Sleep(1000);
maxWaitTimeInSeconds--;
}
if (maxWaitTimeInSeconds == 0)
{
Console.WriteLine("RestoreObject is timeout. ");
throw new TimeoutException();
}
else
{
Console.WriteLine("RestoreObject is successful. ");
}