はじめに
Amazon S3(以下、S3)とは、Amazon Web Services によって提供されるオンラインストレージのWebサービスです。アクセスする手法の一つにAWSコマンドラインインタフェース(以下、awscli)があります。awscliを使用することで、通常のLinuxでのファイル操作(ls、cp等)と同等の処理を、S3に対して実行することが可能です。例えばS3内のバケットの一覧を取得するには次のように実行します。
~$ aws s3 ls
あるプロジェクトでS3のファイルの内容を確認したいが、ファイルのコピー自体はしたくないとの要求がありました。通常のLinuxでは、catコマンドを利用することで、ファイルの内容を標準出力へ出力することができます。これをawscliで実現できないか調査したところ、標準入力/標準出力はハイフン(-)で表記することで、ファイルストリームによる入出力ができることがわかりました。
awscliについて
awscliのバージョンは、1.11.164です。
[ec2-user@ip-xxx-xxx-xxx-xxx ~]$ aws --version aws-cli/1.11.164 Python/2.7.12 Linux/4.9.27-14.31.amzn1.x86_64 botocore/1.7.22 [ec2-user@ip-xxx-xxx-xxx-xxx ~]$
S3のawscliコマンドについて
awscliでS3に対する操作を実施するコマンドの一覧です。
[ec2-user@ip-xxx-xxx-xxx-xxx ~]$ aws s3 cat usage: aws [options][ ...] [parameters] To see help text, you can run: aws help aws <command> help aws <command> <subcommand> help aws: error: argument subcommand: Invalid choice, valid choices are: ls | website cp | mv rm | sync mb | rb
以上のように、catに相当するコマンドはありません。。。
S3での標準出力記法について
cpコマンドのヘルプを見ると最後に以下の記載があります。
The following cp command uploads a local file stream from standard input to a specified bucket and key: aws s3 cp - s3://mybucket/stream.txt Downloading an S3 object as a local file stream WARNING:: PowerShell may alter the encoding of or add a CRLF to piped or redirected output. The following cp command downloads an S3 object locally as a stream to standard output. Downloading as a stream is not currently compatible with the --recursive parameter: aws s3 cp s3://mybucket/stream.txt -
ハイフン(-)が標準入力と標準出力の表記方法なんですね。
実験
バケットの作成
以下のコマンドを実行し、試験用のバケットを作成します。
(試す場合は、バケット名は任意です)
[ec2-user@ip-xxx-xxx-xxx-xxx ~]$ aws s3 mb s3://com.newral-tech.bucket make_bucket: com.newral-tech.bucket [ec2-user@ip-xxx-xxx-xxx-xxx ~]$
ファイルを作成
以下のコマンドを実行し、試験用のファイルを作成します。
[ec2-user@ip-xxx-xxx-xxx-xxx ~]$ aws s3 cp - s3://com.newral-tech.bucket/test.txt you got it! [ec2-user@ip-xxx-xxx-xxx-xxx ~]$
上記操作にてyou got it!という内容のtext.txtファイルが、com.newral-tech.bucket内に作成されます。
EOFの入力方法は端末ソフトに依存しています(puttyの場合はCtrl+dでした)
ファイル内容を確認
以下のコマンドを入力し、ファイルの内容を確認できました。
[ec2-user@ip-xxx-xxx-xxx-xxx ~]$ aws s3 cp s3://com.newral-tech.bucket/test.txt - you got it! [ec2-user@ip-xxx-xxx-xxx-xxx ~]$