AWS S3 Glacier - Initiate Recovery Programmatically

I am writing a web application using s3 for storage and glacier for backup. Therefore, I am setting up a lifecycle policy to archive it. Now I want to write a webapp that lists archive files, the user should be able to initiate a recovery from this, and then receive an email after the recovery is complete.

Now the problem that I am facing is that I cannot find the php sdk command that I can execute for initiateRestore. Then it would be nice if he notified SNS when the recovery was completed, SNS would push JSON to SQS, and I would poll SQS and finally send an email to the user when the poll found a full recovery.

Any help or suggestions would be nice. Thanks.

+6
source share
2 answers

You can also use the AWS CLI tool (here I assume that 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 

As for your desire for notification, the CLI tool will report “A client error has occurred (RestoreAlreadyInProgress): object recovery is already in progress” if the request has already been started, and possibly another message after it has been restored. You can run this restore command several times by looking for the "restore done" error message. Pretty hacked, of course; probably the best way with the AWS CLI tool.

Caution: be careful with Glacier recovery exceeding the allotted amount / free recovery period. If you restore too much data too quickly, fees can exponentially accumulate.

+7
source

I wrote something pretty similar. I cannot speak with any PHP api, however there is a simple http POST that triggers glacier recovery .

Since this happens asynchronously (and takes up to 5 hours), you need to configure the process of polling files restored using HEAD for an object that will have information about the recovery status in the x-amz-restore header.

If this helps, my ruby ​​code to parse this header is as follows:

  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 
+2
source

Source: https://habr.com/ru/post/954523/


All Articles