AWS S3 Glacier - Programmatically Initiate Restore

2020-06-17 10:16发布

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.

2条回答
贼婆χ
2楼-- · 2020-06-17 10:55

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.

查看更多
▲ chillily
3楼-- · 2020-06-17 10:58

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
查看更多
登录 后发表回答