All Products
Search
Document Center

Object Storage Service:Restore objects

Last Updated:Dec 07, 2023

In a versioned Object Storage Service (OSS) bucket, the storage classes of different versions of an object can be different. By default, when you call the RestoreObject operation to restore an object, the current version of the object is restored. You can specify a version ID in the request to restore a specific version of an object.

Usage notes

  • In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS by using other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about the regions and endpoints supported by OSS, 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 an object, you must have the oss:RestoreObject permission. For more information, see Attach a custom policy to a RAM user.

Sample code

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

using System;
using System.Threading;
using Aliyun.OSS;

namespace Samples
{
    public class Program
    {
        public static void Main(string[] args)
        {
            // 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 = "https://oss-cn-hangzhou.aliyuncs.com";
            // 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 name of the bucket. Example: examplebucket. 
            var bucketName = "examplebucket";
            // Specify the full path of the object. Do not include the bucket name in the full path. Example: exampledir/exampleobject.txt. 
            var objectName = "exampledir/exampleobject.txt";
            // Specify the version ID of the Archive object. 
            var versionid = "yourArchiveObjectVersionid";
            int maxWaitTimeInSeconds = 600;
            // Create an OSSClient instance. 
            var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
            var getrequest = new GetObjectMetadataRequest(bucketName, objectName)
            {
                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)
            {
                VersionId = versionid
            };
            var result = client.RestoreObject(request);
            Console.WriteLine("RestoreObject result HttpStatusCode : {0}, versionid: {1}", result.HttpStatusCode, result.VersionId);

            while (maxWaitTimeInSeconds > 0)
            {
                var req = new GetObjectMetadataRequest(bucketName, objectName)
                {
                    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. ");
            }
        }
    }
}

References

For more information about the API operation that you can call to restore an object, see RestoreObject.