Amazon Simple Storage Service
开发人员指南 (API Version 2006-03-01)
AWS 服务或AWS文档中描述的功能,可能因地区/位置而异。请点击 Amazon AWS 入门,可查看中国地区的具体差异

使用 适用于 .NET 的 AWS 开发工具包 还原存档对象

下面的任务将指导您完成使用适用于 .NET 的 AWS 开发工具包启动存档对象的还原操作的过程。

下载对象

1

创建 AmazonS3 类的实例。

2

通过提供存储桶名称、用于恢复的对象键和您要用于恢复对象副本的天数,创建 RestoreObjectRequest 类的实例。

3

执行 AmazonS3.RestoreObject 方法之一,以启动存档恢复。

以下 C# 代码示例演示了上述任务。

Copy
IAmazonS3 client; string bucketName = "examplebucket"; string objectKey = "examplekey"; client = new AmazonS3Client(Amazon.RegionEndpoint.USEast1); RestoreObjectRequest restoreRequest = new RestoreObjectRequest() { BucketName = bucketName, Key = objectKey, Days = 2 }; client.RestoreObject(restoreRequest);

Amazon S3 将在对象元数据中保存还原状态。您可以检索对象元数据并检查 RestoreInProgress 属性的值,如下面的 C# 代码段所示。

Copy
IAmazonS3 client; string bucketName = "examplebucket"; string objectKey = "examplekey"; client = new AmazonS3Client(Amazon.RegionEndpoint.USEast1); GetObjectMetadataRequest metadataRequest = new GetObjectMetadataRequest() { BucketName = bucketName, Key = objectKey }; GetObjectMetadataResponse response = client.GetObjectMetadata(metadataRequest); Console.WriteLine("Restoration status: {0}", response.RestoreInProgress); if (response.RestoreInProgress == false) Console.WriteLine("Restored object copy expires on: {0}", response.RestoreExpiration);

以下 C# 代码示例将为指定的存档对象启动恢复请求。您必须更新代码并提供存储桶名称和存档的对象键名称。有关如何创建和测试有效示例的说明,请参阅 运行 Amazon S3 .NET 代码示例

Copy
using System; using Amazon.S3; using Amazon.S3.Model; namespace s3.amazon.com.docsamples { class RestoreArchivedObject { static string bucketName = "*** provide bucket name ***"; static string objectKey = "*** archived object keyname ***"; static IAmazonS3 client; public static void Main(string[] args) { try { using (client = new AmazonS3Client(Amazon.RegionEndpoint.USEast1)) { RestoreObject(client, bucketName, objectKey); CheckRestorationStatus(client, bucketName, objectKey); } Console.WriteLine("Example complete. To continue, click Enter..."); Console.ReadKey(); } catch (AmazonS3Exception amazonS3Exception) { Console.WriteLine("S3 error occurred. Exception: " + amazonS3Exception.ToString()); } catch (Exception e) { Console.WriteLine("Exception: " + e.ToString()); } } static void RestoreObject(IAmazonS3 client, string bucketName, string objectKey) { RestoreObjectRequest restoreRequest = new RestoreObjectRequest { BucketName = bucketName, Key = objectKey, Days = 2 }; RestoreObjectResponse response = client.RestoreObject(restoreRequest); } static void CheckRestorationStatus(IAmazonS3 client, string bucketName, string objectKey) { GetObjectMetadataRequest metadataRequest = new GetObjectMetadataRequest { BucketName = bucketName, Key = objectKey }; GetObjectMetadataResponse response = client.GetObjectMetadata(metadataRequest); Console.WriteLine("Restoration status: {0}", response.RestoreInProgress); if (response.RestoreInProgress == false) Console.WriteLine("Restored object copy expires on: {0}", response.RestoreExpiration); } } }