Skip to content

Instantly share code, notes, and snippets.

@mikeadmire
Created November 6, 2014 19:25
Show Gist options
  • Save mikeadmire/6031cb6e0210154fda98 to your computer and use it in GitHub Desktop.
Save mikeadmire/6031cb6e0210154fda98 to your computer and use it in GitHub Desktop.
Read a list of files from a text file and use the ruby AWS SDK to restore them from Glacier storage.
#!/usr/bin/env ruby
require 'aws-sdk'
require 'logger'
s3 = AWS::S3.new(
access_key_id: '<ACCESS KEY>',
secret_access_key: '<SECRET KEY>'
)
logger = Logger.new('./s3_restores.log')
bucket = s3.buckets['<BUCKET NAME>']
File.foreach('./files_to_restore.txt') do |line|
line.chomp!
logger.info("restoring #{line}")
begin
object = bucket.objects[line]
object.restore(options = {days: 5})
rescue => e
logger.error("#{e}:: #{line}")
end
end
@mikeadmire
Copy link
Author

The text file with file names to be restored should be the full object name (full path) excluding the bucket name.

Example:

images/123456/image001.jpg
images/123456/image002.jpg
images/123456/image003.jpg

All restores from Glacier are temporary. The default for the SDK is 1 day (last I checked), so I'm overwriting that to 5 days here. In order to permanently restore a file from Glacier you need to change the storage type from Glacier. One method of doing this is to execute a copy on the file or bucket to overwrite the existing Glacier file with the restored file. This only works after the file has been restored.

Change storage class for one file:

aws s3 cp s3://<BUCKET NAME>/<FILE PATH> s3://<BUCKET NAME>/<FILE PATH> --storage-class STANDARD

Change storage class for all files in a bucket:

aws s3 cp s3://<BUCKET NAME> s3://<BUCKET NAME> --storage-class STANDARD --recursive

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment