toto_1212

技術のログをツラツラ書きます。自分用ですが参考にしていただけたら嬉しいです。間違ってたらドンドン突っ込んでください。

s3cmdでS3をコマンドラインで操作

環境:amazon Linux 64bit
   S3backet名 mnttest
※rootで作業

1. s3cmdをインストールする

yum --enablerepo epel install s3cmd

2. AWSアカウントのアクセスIDとシークレットアクセスキーを設定する

s3cmd --configure

Access Key: xxxxxx
Secret Key: xxxxxx
Encryption password: 			#GPG暗号化のパスワード設定(入れなくても可)
Path to GPG program [/usr/bin/gpg]:	#GPGプログラムの場所(そのままでOK)
Use HTTPS protocol [No]:	#S3への転送時にHTTPS利用するならyesで
HTTP Proxy server name:		#Proxy環境であれば設定

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]	#Yで接続テスト
Please wait...
Success. Your access key and secret key worked fine :-)		#Success(成功)

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

Save settings? [y/N]		#yで保存する

3.バケット一覧を表示してみる

s3cmd ls

★結果
[root@testsvr ec2-user]# s3cmd ls
2013-03-16 07:06  s3://cf-templates-xxxx-xx-xxxxxxxx-x
2013-06-10 08:32  s3://mnttest

4.リージョン指定でバケットを作成する

s3cmd mb --bucket-location=ap-northeast-1 s3://3cmdtest
Bucket 's3://3cmdtest/' created

5.作成したバケットの使用量を確認する

s3cmd du s3://3cmdtest
WARNING: Redirected to: 3cmdtest.s3-ap-northeast-1.amazonaws.com
0        s3://3cmdtest/

WARNINGが表示されます。
.s3cfgの設定がUSの方向を向いているかららしいです。

vi .s3cfg

bucket_location = US
host_base = s3.amazonaws.com
host_bucket = %(bucket)s.s3.amazonaws.com

以下のように変更する

bucket_location = ap-northeast-1
host_base = s3-ap-northeast-1.amazonaws.com
host_bucket = %(bucket)s.s3-ap-northeast-1.amazonaws.com

★再確認
[root@testsvr ec2-user]# s3cmd du s3://3cmdtest
0        s3://3cmdtest/		#使ってないので0

6.作成したバケットにファイルをアップロードする

[root@testsvr ec2-user]# s3cmd put test.txt s3://3cmdtest/test1.txt
test.txt -> s3://3cmdtest/test1.txt  [1 of 1]
 5 of 5   100% in    0s     9.40 B/s  done

7.再度、使用量を確認する

[root@testsvr ec2-user]# s3cmd du s3://3cmdtest
5        s3://3cmdtest/

8.アップロードしたファイルを消去する

[root@testsvr ec2-user]# s3cmd del s3://3cmdtest/test1.txt
File s3://3cmdtest/test1.txt deleted

★結果を確認
[root@testsvr ec2-user]# s3cmd du s3://3cmdtest
0        s3://3cmdtest/