LoginSignup
6
4

More than 5 years have passed since last update.

S3のファイルリストを取る方法

Last updated at Posted at 2015-11-25

時々必要になるけど忘れがちな手順を共有します。この手順はコマンドラインから実行する方法です。Windowsでもできると思いますが、コマンド例などはMacのものです。

必要なもの

  • Python 2 version 2.6.5+ or Python 3 version 3.3+
  • pip
  • awscli

How to install

  • ここにある通りですが、
  • pip install awscli

How to get object list

  • 表題ではファイルリストと書きましたが、S3上ではファイルはObjectと呼ばれます
  • aws s3 ls s3://<your bucket name> --profile <your profile> --region ap-northeast-1 --recursive
  • 上記コマンドでObjectのリストが以下のように、標準出力に出力されます
2015-05-22 17:35:08      15338 
2015/20150101/XXXXXXXXXXXXXX43/0000.jpg
2015-05-22 17:35:07      82694 
2015/20150102/XXXXXXXXXXX92043/0001.jpg
2015-05-22 17:35:02      82694 
2015/20150518/XXXXXXXXX5192043/1000.jpg
...
  • JPGファイルだけを出力する場合の例:
aws s3 ls s3://<your bucket name> --profile <your profile> --region ap-northeast-1 --recursive | grep jpg | sed 's/[\t ]\+/\t/g' |  awk -F '/' '{ print $NF }'

How to set profile

  • ~/.aws/credentials に以下の内容を定義
[your profilename]
aws_access_key_id = AXXXXXXXXXXX
aws_secret_access_key = PXXXXXXXXXXXXXXXXXXXXXXXXXXXX
6
4
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
6
4