Restore objects (OSS SDK for C# 1.0)

Updated at:

In a bucket with versioning enabled, different versions of an object can have different storage classes. By default, the RestoreObject API operation restores the current version of an object. To restore a specific version, you must specify its versionId.

Notes

  • 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.

  • This topic demonstrates creating an OSSClient instance with an OSS endpoint. For alternative configurations, such as using a custom domain or authenticating with credentials from Security Token Service (STS), see Initialization (C# SDK V1).

  • To restore an object, you must have the oss:RestoreObject permission. For more information, see Grant a custom policy.

Sample code

The following code restores an object from Archive, Cold Archive, or Deep Cold Archive storage:

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;
            // 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 based on your requirements.
            var conf = new ClientConfiguration();
            
            // Use the signature algorithm V4.
            conf.SignatureVersion = SignatureVersion.V4;
            
            // Create an OSSClient instance.
            var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);
            client.SetRegion(region);
            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.