s3cmdをUbuntuにインストール

http://s3tools.org/repositories

$ wget -O- -q http://s3tools.org/repo/deb-all/stable/s3tools.key | sudo apt-key add -
$ sudo wget -O/etc/apt/sources.list.d/s3tools.list http://s3tools.org/repo/deb-all/stable/s3tools.list
$ sudo apt-get update && sudo apt-get install s3cmd

とりあえずAccess KeyとSecret Keyが必要です.IAMでS3へのアクセス権のみを持ったアカウントを一つ作ります.

  1. ダッシュボードからIAMを選択(S3じゃない)
  2. Create New Usersで適当な名前を入れる.
  3. credentials.csvを必ずダウンロードしておきます.中にはscretidがふくまれています.中身は以下のようになっています.

     User Name,Access Key Id,Secret Access Key
     "iaarchiver_s3",********************,************************
  4. 一覧からuserを選択し,permissionsのタブからUser Policyを追加します.ここではテンプレートからS3fullaccessを選択する

アクセスキーとシークレットキーを手に入れたらs3cmdの初期設定を行います.

$ s3cmd --configure

Enter new values or accept defaults in brackets with Enter.
Refer to user manual for detailed description of all options.

Access key and Secret key are your identifiers for Amazon S3
Access Key: <<アクセスキーを入力>>
Secret Key: <<シークレットキーを入力>>

Encryption password is used to protect your files from reading
by unauthorized persons while in transfer to S3
Encryption password: <<パスワード入力>>
Path to GPG program [/usr/bin/gpg]: 

When using secure HTTPS protocol all communication with Amazon S3
servers is protected from 3rd party eavesdropping. This method is
slower than plain HTTP and can't be used if you're behind a proxy
Use HTTPS protocol [No]: 

On some networks all internet access must go through a HTTP proxy.
Try setting it here if you can't conect to S3 directly
HTTP Proxy server name: 

New settings:
  Access Key: *********************
  Secret Key: ************************************
  Encryption password: *************
  Path to GPG program: /usr/bin/gpg
  Use HTTPS protocol: False
  HTTP Proxy server name: 
  HTTP Proxy server port: 0

Test access with supplied credentials? [Y/n] 
Please wait, attempting to list all buckets...
Success. Your access key and secret key worked fine :-)

Now verifying that encryption works...
Success. Encryption and decryption worked fine :-)

Save settings? [y/N] y
Configuration saved to '/home/iaarchiver/.s3cfg'

基本コマンド&トラブルの覚書

ファイルを送る

s3cmd put test.txt s3://mybacket

ファイルを受け取る

s3cmd get s3://mybacket .

ファイルの一覧を見る

s3cmd ls s3://mybacket

ファイルを削除する

s3cmd del s3://mybacket 

ディレクトリを同期する

s3cmd sync ~/local_backet s3://mybacket/

新しいバケットを作成する

s3cmd mb s3://mybacket

バケットを削除する(空にする必要があり)

s3cmd rb s3://mybacket

設定ファイル(.s3cfg)を指定する

s3cmd -c [--config] $HOME/.config/s3cfg ...

エラーが出てバケットが作れないとき

The requested bucket name is not available. The bucket namespace is shared by all users of the system. Please select a different name and try again.

というエラーが出るとき,バケット名でエラーが起きています.aws全体で名前空間が共有されているので先に他の人が同名のバケットを作ってしまっているとダメ.

WARNING: Redirected to...と出る

http://xoxo-infra.hatenablog.com/entry/2013/02/06/020930

~/.s3cfg内で設定されているロケールを日本のものに変更

特定のフォルダを同期させたくないとき

例えば.gitなど いくつかやりかたはありそう.s3cmd -hで確認 例えば不可視フォルダ(.から始まる)は同期しないやり方は

s3cmd sync --exclude='^(.*/)*\.+.*' ~/.dir_synced s3://mybacket

mirrorしたいとき

--delete-removedをつける ミラーしない場合は --no-delete-removedと明示する

デプロイ用のスクリプトを作成する

簡単な例は例えば以下の様なかんじ.

#!/bin/bash

TARGET="$HOME/Dropbox/Codes/Grunt/scriptogr.am/"
BUCKET="s3://s3.iaarchiver.net/"

# ignore invisible file/dir
s3cmd -c $HOME/.config/.s3cfg sync \
        --rexclude="^(.*/)*\.+.*" \
        --recursive \
        --delete-removed \
        $TARGET $BUCKET

このとき$TARGETで指定したフォルダ内のコンテンツが再帰的にS3のバケットにミラーリングされる.