Restore all Glacier objects in S3 bucket

Posted by Joe Enos on March 25, 2021 · 4 mins read

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>