Restore all Glacier objects in S3 bucket

If you’ve got an entire bucket in S3 with items in Glacier or Glacier Deep Archive storage class, and you want to restore them to download them, this C# code will restore all of the files. Just set the constant values at the top and it’ll go through the items one-by-one and submit a restore request.

No notifications or anything when the restore is done, so maybe just give it a day or so and come back.

using static System.Console;
using Amazon;
using Amazon.Runtime;
using Amazon.S3;
using Amazon.S3.Model;

const string ACCESS_KEY = "XXXXXXXXXXXXXXXX";
const string SECRET_KEY = "XXXXXXXXXXXXXXXX";
var regionEndpoint = RegionEndpoint.XXXXXXXXXXXXXXXX;
const string BUCKET_NAME = "XXXXXXXXXXXXXXXX";
const int NUM_DAYS = 15;

var client = new AmazonS3Client(new BasicAWSCredentials(ACCESS_KEY, SECRET_KEY),
regionEndpoint);

var request = new ListObjectsRequest { BucketName = BUCKET_NAME };
ListObjectsResponse items;
do
{
items = await client.ListObjectsAsync(request);
foreach (var item in items.S3Objects)
{
Write(item.Key);
try
{
await client.RestoreObjectAsync(new RestoreObjectRequest
{
BucketName = BUCKET_NAME,
Key = item.Key,
Days = NUM_DAYS
}).ConfigureAwait(false);
}
catch
{
WriteLine("Fail...");
}
WriteLine("...DONE");
}
request = new ListObjectsRequest
{
BucketName = BUCKET_NAME,
Marker = items.NextMarker
};
} while (!string.IsNullOrEmpty(items.NextMarker));
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AWSSDK.S3" Version="3.5.6.2" />
</ItemGroup>

</Project>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s