AWS S3 Glacier - Programmatically Initiate Restore

一笑奈何 提交于 2019-12-03 14:20:36

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.

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!