LoginSignup
2
1

More than 1 year has passed since last update.

Amazon S3 Glacier Deep Archiveからデータを復元する

Posted at

やりたいこと

タイトル通り、Amazon S3 Glacier Deep Archiveからデータを復元して取り出したいです。

基本

restore-objectコマンドを使います。

次の例では、30日間 [awsexamplebucket] 内の [dir1/exa-1xxxxxxx/example.mp4] を復元します。(s3://awsexamplebucket/dir1/exa-1xxxxxxx/example.mp4)

aws s3api restore-object --bucket awsexamplebucket --key dir1/exa-1xxxxxxx/example.mp4 --restore-request '{"Days":30,"GlacierJobParameters":{"Tier":"Standard"}}' --profile my-profile

--keyのパラメータにはS3 URIのbucket名の後ろからの文字列(この例の場合dir1/exa-1xxxxxxx/example.mp4)を入れればOK。

バケット内にオブジェクトがたくさんあって一気にやりたい場合

バケットの中にディレクトリ的なもの(S3には実際はディレクトリという概念は無いんですよね)が何件もある場合。

今回は、awsexamplebucketというバケットの/dir1/にexa-xxxxxxxxというディレクトリ的なものが複数あって、そのそれぞれの直下に1つexample.mp4が入っているという想定。

- awsexamplebucket
  - dir1
    - exa-1xxxxxxx
      - example.mp4
    - exa-2xxxxxxx
      - example.mp4
    - exa-3xxxxxxx
      - example.mp4
  - dir2

手始めに、オブジェクトの一覧はaws s3 lsコマンドで参照できるのでやってみる。

$ aws s3 ls --profile my-profile s3://awsexamplebucket/dir1/
                           PRE exa-1xxxxxxx/
                           PRE exa-2xxxxxxx/
                           PRE exa-3xxxxxxx/
                           PRE exa-4xxxxxxx/
                           PRE exa-5xxxxxxx/
                           PRE exa-6xxxxxxx/
                           PRE exa-7xxxxxxx/

では、いよいよこれらのファイルを一気に復元します。
今回はシェルスクリプトでやります。

test.sh
#!/bin/sh

aws s3 ls --profile my-profile "s3://awsexamplebucket/dir1" | awk '{print $2}' | while read file
do
  aws s3api restore-object --bucket awsexamplebucket --key dir1/${file}example.mp4 --restore-request '{"Days":14,"GlacierJobParameters":{"Tier":"Standard"}}' --profile my-profile
done

実行。

terminal
sh test.sh

S3のコンソール画面を見ると、成功したらオブジェクトのページがこんな感じになって、復元がスタートします。
スクリーンショット 2022-07-26 17.41.32.png

復元が無事に完了したらこんな感じになりました。
スクリーンショット 2022-07-27 20.34.38.png

めでたし。めでたし。

蛇足:署名付きURL

ちなみにDeep archiveのオブジェクトに署名付きURLを発行して、解凍前にアクセスしようとすると以下のようなエラーになりました。

<Error>
<Code>InvalidObjectState</Code>
<Message>The operation is not valid for the object's storage class</Message>
<StorageClass>DEEP_ARCHIVE</StorageClass>
<RequestId>省略</RequestId>
<HostId>省略</HostId>
</Error>

あるバケット内のオブジェクト全てに対して署名付きURLを発行するのもシェルスクリプトで行っていたので、メモメモ。

#!/bin/sh

aws s3 ls --profile my-profile "s3://awsexamplebucket/dir1/" | awk '{print $2}' | while read file
do
  aws s3 presign s3://awsexamplebucket/dir1/${file}example.mp4 --expires-in 259200 --profile my-profile
done
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1