LoginSignup
5
5

More than 5 years have passed since last update.

Terminal.appからS3内のフォルダの中身を別フォルダにコピーする

Last updated at Posted at 2014-08-19

はじめに

S3にはフォルダという概念はなくて、folder/foobar.pngというファイル名だったと思ったが、Management Consoleを確認してみたら

Create Folder...

となっていたので、フォルダという表記で進める。

やりたいこと

Bucket内のfooフォルダの中に1万ほどファイルがあって、これをbarフォルダにコピーしたい。

Copy/Paste Into

Management ConsoleにCopyPaste Intoというメニューがあったので、これを使ったらできるかなと思ったけど無理でした。さて、バッチでも書くか。と思ったらどうやらaws-cliを入れるとTerminalから

$aws s3 sync s3://bucket-src s3://bucket-target --exclude *.tmp

みたいな感じで操作ができるみたい。

aws-cliのインストール

pipでインストールできる。

$ pip install awscli

接続情報の設定

configureコマンドでインタラクティブに設定する。

$ aws configure
Traceback (most recent call last):
#-*- snip -*-
ValueError: unknown locale: UTF-8

と思ったらエラー。どうやら、localeの設定ができていないよう。

$ locale
LANG=
LC_COLLATE="C"
LC_CTYPE="UTF-8"
LC_MESSAGES="C"
LC_MONETARY="C"
LC_NUMERIC="C"
LC_TIME="C"
LC_ALL=

言語の設定を入れてあげる。設定できるlocaleはlocale -aで確認できる。今回は、ja_JP.UTF-8を設定することにした。そして、Terminalを閉じる度に設定するのも面倒なので.bash_profileに設定を反映させた。

$ echo 'export LC_ALL=a_JP.UTF-8' >> ~/.bash_profile
$ echo 'export LANG=a_JP.UTF-8' >> ~/.bash_profile
$ source ~/.bash_profile

再度configureコマンドを実行してみる。

$ aws configure
AWS Access Key ID [None]: ********************
AWS Secret Access Key [None]: ****************************************
Default region name [None]: ap-northeast-1
Default output format [None]: 

Asia Pacific (Tokyo) Regionはap-northeast-1。それ以外は、Regions and Endpoints - Amazon Web Servicesで確認する。

Auto Completionの設定

Tabでコマンドの補完を行えるように設定する。zshやtcshの人は公式ドキュメントに従って設定する。bashの人はcompleteコマンドを利用する。

$ echo 'complete -C aws_completer aws' >> ~/.bash_profile
$ source ~/.bash_profile

aws-cliの利用

bucketの中身の確認

$ aws s3 ls s3://mybucket
                           PRE foo/
                           PRE bar/
2014-05-27 11:52:47       3298 foobar.png

フォルダのコピー

$ aws s3 sync s3://mybucket/foo s3://mybucket/bar
copy: s3://mybucket/foo/47484652.jpg to s3://mybucket/bar/47484652.jpg
copy: s3://mybucket/foo/47484653.jpg to s3://mybucket/bar/47484653.jpg
copy: s3://mybucket/foo/47484654.jpg to s3://mybucket/bar/47484654.jpg
...

コピー時の注意

  1. 一ファイルずつコピーのログが表示されるので、大量のファイルをコピーする際は--quietオプションをつけたほうがいい。
  2. 同一ファイル名があった場合、ファイルサイズが違う場合は上書き、そうでない場合はスキップされる。
  3. ただし、同一ファイル名でファイルサイズも同じ場合でも、コピー元のファイル作成日が新しい場合は上書きされる。

参考サイト

AWS S3 copy files and folders between two buckets
Pelican 3.3 pelican-quickstart error “ValueError: unknown locale: UTF-8”

5
5
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
5
5