Arşiv veya Soğuk Arşiv depolama sınıfındaki bir nesneyi okumadan önce bu nesneyi geri yüklemeniz gerekir. Bu konu, Arşiv veya Soğuk Arşiv nesnelerinin nasıl geri yükleneceğini açıklar.
Notlar
RestoreObject işlemi yalnızca Arşiv ve Soğuk Arşiv nesnelerinde çağrılabilir.
Bu konuda Çin (Hangzhou) bölgesinin genel uç noktası kullanılmıştır. OSS ile aynı bölgedeki diğer Alibaba Cloud hizmetlerinden OSS'e erişmek için dahili uç nokta kullanın. OSS bölgeleri ve uç noktaları hakkında daha fazla bilgi için bkz. Bölgeler ve uç noktalar.
Bu konuda bir OSS uç noktası kullanılarak OSSClient örneğinin oluşturulması gösterilmektedir. Özel etki alanı kullanma veya Security Token Service (STS) kimlik bilgileriyle kimlik doğrulama gibi alternatif yapılandırmalar için bkz. Başlatma (C# SDK V1).
Nesneleri geri yüklemek için
oss:RestoreObjectiznine sahip olmanız gerekir. Daha fazla bilgi için bkz. Özel ilke atama.
Arşiv nesnesini geri yükleme
Aşağıdaki örnek kod, Arşiv nesnesinin nasıl geri yükleneceğini gösterir:
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 name of the bucket.
var bucketName = "examplebucket";
// Specify the full path of the object. The path cannot contain the bucket name.
var objectName = "yourObjectName";
// Specify the content of the object.
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 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);
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 to the bucket and set the storage class of the object 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);
// The maximum wait time decreases by 1 second.
maxWaitTimeInSeconds--;
}
if (maxWaitTimeInSeconds == 0)
{
Console.WriteLine("RestoreObject is timeout. ");
throw new TimeoutException();
}
else
{
Console.WriteLine("RestoreObject is successful. ");
}
Soğuk Arşiv nesnesini geri yükleme
Aşağıdaki kod, Soğuk Arşiv nesnesinin nasıl geri yükleneceğine ilişkin bir örnek sunar:
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 name of the bucket.
var bucketName = "examplebucket";
// Specify the full path of the Cold Archive object. Do not include the bucket name in the full path.
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 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);
// Restore the Cold Archive object.
var request = new RestoreObjectRequest(bucketName, objectName);
// Specify the restoration priority of 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 restoration parameters. For example, you can set the restoration priority of the object to Expedited, and set the duration for which the object can remain in the restored state to two days.
// The Days parameter indicates the duration for which the object can remain in the restored state. The default value is 1. This parameter applies to Archive and Cold Archive objects.
// The Tier parameter indicates the restoration priority of the object and 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);
İlgili konular
Arşiv ve Soğuk Arşiv nesnelerini geri yüklemek için kullanılan eksiksiz örnek kodu görüntülemek üzere GitHub adresini ziyaret edin.
Arşiv ve Soğuk Arşiv nesnelerini geri yüklemek için çağırabileceğiniz API işlemi hakkında daha fazla bilgi için bkz. RestoreObject.