Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS S3 Glacier - Programmatically Initiate Restore

I have been writing an web-app using s3 for storage and glacier for backup. So I setup the lifecycle policy to archive it. Now I want to write a webapp that lists the archived files, the user should be able to initiate restore from this and then get an email once their restore is complete.

Now the trouble I am running into is I cant find a php sdk command I can issue to initiateRestore. Then it would be nice if it notified SNS when restore was complete, SNS would push the JSON onto SQS and I would poll SQS and finally email the user when polling detected a complete restore.

Any help or suggestions would be nice. Thanks.

like image 867
Ramuk Avatar asked Sep 24 '13 16:09

Ramuk


People also ask

How do you restore multiple objects from a Glacier?

For Operation, select Restore. For Restore source, select Glacier or Glacier Deep Archive. For Number of days that the restored copy is available, enter the number of days for your use case. For Restore tier, select either Bulk retrieval or Standard retrieval.

How do I retrieve from Glacier archive?

Retrieving an archive from S3 Glacier is a two-step process. Initiate an archive retrieval job. Get the ID of the archive that you want to retrieve. You can get the archive ID from an inventory of the vault.

What is the correct retrieval time for restoring objects from Amazon S3 Glacier flexible retrieval?

Standard retrievals typically finish within 3–5 hours for objects stored in the S3 Glacier Flexible Retrieval storage class or S3 Intelligent-Tiering Archive Access tier. They typically finish within 12 hours for objects stored in the S3 Glacier Deep Archive or S3 Intelligent-Tiering Deep Archive Access storage class.


2 Answers

You could also use the AWS CLI tool like so (here I'm assuming you want to restore all files in one directory):

aws s3 ls s3://myBucket/myDir/ | awk '{if ($4) print $4}' > myFiles.txt
for x in `cat myFiles.txt`
do
    echo "restoring $x"
    aws s3api restore-object \
        --bucket myBucket \
        --key "myDir/$x" \
        --restore-request '{"Days":30}'
done

Regarding your desire for notification, the CLI tool will report "A client error (RestoreAlreadyInProgress) occurred: Object restore is already in progress" if request already initiated, and probably a different message once it restores. You could run this restore command several times, looking for "restore done" error/message. Pretty hacky of course; there's probably a better way with AWS CLI tool.

Caveat: be careful with Glacier restores that exceed the allotted free-restore amount/period. If you restore too much data too quickly, charges can exponentially pile up.

like image 167
Dolan Antenucci Avatar answered Sep 28 '22 11:09

Dolan Antenucci


I wrote something fairly similar. I can't speak to any PHP api, however there's a simple http POST that kicks off glacier restoration.

Since that happens asyncronously (and takes up to 5 hours), you have to set up a process to poll files that are restoring by making HEAD requests for the object, which will have restoration status info in an x-amz-restore header.

If it helps, my ruby code for parsing this header looks like this:

    if restore = headers['x-amz-restore']
      if restore.first =~ /ongoing-request="(.+?)", expiry-date="(.+?)"/
        restoring = $1 == "true"
        restore_date = DateTime.parse($2)
      elsif restore.first =~ /ongoing-request="(.+?)"/
        restoring = $1 == "true"
      end
    end
like image 23
Bradley Schaefer Avatar answered Sep 28 '22 12:09

Bradley Schaefer