在受版本控制的存储空间(Bucket)中,文件(Object)的各个版本可以对应不同的存储类型。RestoreObject接口默认解冻Object的当前版本,允许通过指定versionId的方式来解冻指定版本的Object。

示例代码

以下代码用于解冻文件:

using Aliyun.OSS;
using Aliyun.OSS.Model;
// yourEndpoint填写Bucket所在地域对应的Endpoint。以华东1(杭州)为例,Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。
var endpoint = "yourEndpoint";
// 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。
var accessKeyId = "yourAccessKeyId";
var accessKeySecret = "yourAccessKeySecret";
// 填写Bucket名称,例如examplebucket。
var bucketName = "examplebucket";
// 填写Object完整路径,完整路径中不能包含Bucket名称,例如exampledir/exampleobject.txt。
var objectName = "exampledir/exampleobject.txt";
var objectContent = "More than just cloud.";
// 指定归档类型Object的版本ID。
var versionid = "yourArchiveObjectVersionid";
int maxWaitTimeInSeconds = 600;
// 创建OSSClient实例。
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;
}
// 解冻文件。
var request = new RestoreObjectRequest(bucketName, objectName)
{   
    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)
    {
        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. ");
}

相关文档

关于解冻文件的API接口说明,请参见RestoreObject