-
Notifications
You must be signed in to change notification settings - Fork 5.7k
/
RestoreArchivedObject.cs
86 lines (76 loc) · 3.4 KB
/
RestoreArchivedObject.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
namespace RestoreArchivedObjectExample
{
// snippet-start:[S3.dotnetv3.RestoreArchivedObjectExample]
using System;
using System.Threading.Tasks;
using Amazon;
using Amazon.S3;
using Amazon.S3.Model;
/// <summary>
/// This example shows how to restore an archived object in an Amazon
/// Simple Storage Service (Amazon S3) bucket.
/// </summary>
public class RestoreArchivedObject
{
public static void Main()
{
string bucketName = "amzn-s3-demo-bucket";
string objectKey = "archived-object.txt";
// Specify your bucket region (an example region is shown).
RegionEndpoint bucketRegion = RegionEndpoint.USWest2;
IAmazonS3 client = new AmazonS3Client(bucketRegion);
RestoreObjectAsync(client, bucketName, objectKey).Wait();
}
/// <summary>
/// This method restores an archived object from an Amazon S3 bucket.
/// </summary>
/// <param name="client">The initialized Amazon S3 client object used to call
/// RestoreObjectAsync.</param>
/// <param name="bucketName">A string representing the name of the
/// bucket where the object was located before it was archived.</param>
/// <param name="objectKey">A string representing the name of the
/// archived object to restore.</param>
public static async Task RestoreObjectAsync(IAmazonS3 client, string bucketName, string objectKey)
{
try
{
var restoreRequest = new RestoreObjectRequest
{
BucketName = bucketName,
Key = objectKey,
Days = 2,
};
RestoreObjectResponse response = await client.RestoreObjectAsync(restoreRequest);
// Check the status of the restoration.
await CheckRestorationStatusAsync(client, bucketName, objectKey);
}
catch (AmazonS3Exception amazonS3Exception)
{
Console.WriteLine($"Error: {amazonS3Exception.Message}");
}
}
/// <summary>
/// This method retrieves the status of the object's restoration.
/// </summary>
/// <param name="client">The initialized Amazon S3 client object used to call
/// GetObjectMetadataAsync.</param>
/// <param name="bucketName">A string representing the name of the Amazon
/// S3 bucket which contains the archived object.</param>
/// <param name="objectKey">A string representing the name of the
/// archived object you want to restore.</param>
public static async Task CheckRestorationStatusAsync(IAmazonS3 client, string bucketName, string objectKey)
{
GetObjectMetadataRequest metadataRequest = new GetObjectMetadataRequest()
{
BucketName = bucketName,
Key = objectKey,
};
GetObjectMetadataResponse response = await client.GetObjectMetadataAsync(metadataRequest);
var restStatus = response.RestoreInProgress ? "in-progress" : "finished or failed";
Console.WriteLine($"Restoration status: {restStatus}");
}
}
// snippet-end:[S3.dotnetv3.RestoreArchivedObjectExample]
}