S3 バケットの中身を tree 形式で表示してくれる s3-tree を Amazon Linux 2 にインストールして使ってみた

tree 形式には抗いがたい魅力がありますよね。

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

コンバンハ、千葉(幸)です。

S3 バケットの中身を…… aws s3 ls の結果を…… tree の、 tree の形式で見たい……!!

皆さんはそんな場面に遭遇したこと、ありませんか?私はつい最近ありました。

ひとまずそんな思いに応えてくれるツールを見つけました。s3-treeです。

今回は Amazon Linux2 の EC2 インスタンスにインストールして S3 バケットを参照し、s3-treeの使用感を確認してみます。

公式のツールではないので、導入の是非は自身で判断しましょう

目次

いつもの aws s3 ls で確認した場合

s3-treeの確認をする前に、AWS CLI の aws s3 lsコマンドを使用した際の表示のイメージを確認しておきましょう。

例えば以下のような構造で S3 バケット内にオブジェクトが格納されているとします。

.
├── fuga
│   ├── fuga1.txt
│   ├── fuga2.txt
│   └── fuga3.txt
├── hoge
│   ├── hoge1.txt
│   └── hoge2.txt
└── piyo
    └── kopiyo
        └── index.html

これを aws s3 lsで参照すると以下のように表示されます。--recursiveオプションを付与することで再帰的に表示できます。

$ aws s3 ls --recursive <S3バケット名>
2020-09-01 21:42:00          0 fuga/fuga1.txt
2020-09-01 21:42:02          0 fuga/fuga2.txt
2020-09-01 21:42:02          0 fuga/fuga3.txt
2020-09-01 21:42:01          0 hoge/hoge1.txt
2020-09-01 21:42:01          0 hoge/hoge2.txt
2020-09-01 21:42:01          0 piyo/kopiyo/index.html

そこまで読み取りづらいというわけでもないですが、ついつい tree 形式に惹かれてしまうのが いきもののサガ というものです。

EC2 インスタンスのセットアップ

今回はクライアントとして EC2 インスタンスを使用します。詳細は割愛しますが、以下のような設定で作成を行いました。

  • amzn2-ami-hvm-2.0.20200722.0-x86_64-gp2 (ami-0cc75a8978fbbc969)
  • t2.micro
  • セッションマネージャーで接続可能な状態にセットアップ
  • S3 へのフルアクセス権限を割り当て

Systems Manager セッションマネージャーで接続したのち、豪快に root にスイッチして、後続の作業をすべて root で行います。検証なので。

事前準備

s3-tree · PyPI

ページを確認すると、dependenciesとして以下が記されています。

  • tree
  • python3
  • aws cli

先にこれらをインストールしておきます。

aws cli

Amazon Linux2 なので、予めインストールされています。

[root@ip-192-168-0-165 ~]# aws --version
aws-cli/1.16.300 Python/2.7.18 Linux/4.14.186-146.268.amzn2.x86_64 botocore/1.13.36

python3 のインストール

yum でインストールしました。

[root@ip-192-168-0-165 ~]# yum install python3
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
Resolving Dependencies
--> Running transaction check
---> Package python3.x86_64 0:3.7.8-1.amzn2.0.1 will be installed
--> Processing Dependency: python3-libs(x86-64) = 3.7.8-1.amzn2.0.1 for package: python3-3.7.8-1.amzn2.0.1.x86_64
--> Processing Dependency: python3-setuptools for package: python3-3.7.8-1.amzn2.0.1.x86_64
--> Processing Dependency: python3-pip for package: python3-3.7.8-1.amzn2.0.1.x86_64
--> Processing Dependency: libpython3.7m.so.1.0()(64bit) for package: python3-3.7.8-1.amzn2.0.1.x86_64
--> Running transaction check
---> Package python3-libs.x86_64 0:3.7.8-1.amzn2.0.1 will be installed
---> Package python3-pip.noarch 0:9.0.3-1.amzn2.0.2 will be installed
---> Package python3-setuptools.noarch 0:38.4.0-3.amzn2.0.6 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

========================================================================================================================================================================
 Package                                      Arch                             Version                                       Repository                            Size
========================================================================================================================================================================
Installing:
 python3                                      x86_64                           3.7.8-1.amzn2.0.1                             amzn2-core                            72 k
Installing for dependencies:
 python3-libs                                 x86_64                           3.7.8-1.amzn2.0.1                             amzn2-core                           9.2 M
 python3-pip                                  noarch                           9.0.3-1.amzn2.0.2                             amzn2-core                           1.9 M
 python3-setuptools                           noarch                           38.4.0-3.amzn2.0.6                            amzn2-core                           617 k

Transaction Summary
========================================================================================================================================================================
Install  1 Package (+3 Dependent packages)

Total download size: 12 M
Installed size: 50 M
Is this ok [y/d/N]: y
Downloading packages:
(1/4): python3-3.7.8-1.amzn2.0.1.x86_64.rpm                                                                                                      |  72 kB  00:00:00
(2/4): python3-pip-9.0.3-1.amzn2.0.2.noarch.rpm                                                                                                  | 1.9 MB  00:00:00
(3/4): python3-setuptools-38.4.0-3.amzn2.0.6.noarch.rpm                                                                                          | 617 kB  00:00:00
(4/4): python3-libs-3.7.8-1.amzn2.0.1.x86_64.rpm                                                                                                 | 9.2 MB  00:00:00
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                    37 MB/s |  12 MB  00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : python3-pip-9.0.3-1.amzn2.0.2.noarch                                                                                                                 1/4
  Installing : python3-3.7.8-1.amzn2.0.1.x86_64                                                                                                                     2/4
  Installing : python3-setuptools-38.4.0-3.amzn2.0.6.noarch                                                                                                         3/4
  Installing : python3-libs-3.7.8-1.amzn2.0.1.x86_64                                                                                                                4/4
  Verifying  : python3-setuptools-38.4.0-3.amzn2.0.6.noarch                                                                                                         1/4
  Verifying  : python3-3.7.8-1.amzn2.0.1.x86_64                                                                                                                     2/4
  Verifying  : python3-pip-9.0.3-1.amzn2.0.2.noarch                                                                                                                 3/4
  Verifying  : python3-libs-3.7.8-1.amzn2.0.1.x86_64                                                                                                                4/4

Installed:
  python3.x86_64 0:3.7.8-1.amzn2.0.1

Dependency Installed:
  python3-libs.x86_64 0:3.7.8-1.amzn2.0.1              python3-pip.noarch 0:9.0.3-1.amzn2.0.2              python3-setuptools.noarch 0:38.4.0-3.amzn2.0.6

Complete!

バージョンはこの通りです。

[root@ip-192-168-0-165 ~]# python3 --version
Python 3.7.8

ついでに pip3 もインストールされています。

[root@ip-192-168-0-165 ~]# pip3 --version
pip 9.0.3 from /usr/lib/python3.7/site-packages (python 3.7)

tree のインストール

こちらも yum でインストールを行います。

[root@ip-192-168-0-165 ~]# yum install tree
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
Resolving Dependencies
--> Running transaction check
---> Package tree.x86_64 0:1.6.0-10.amzn2.0.1 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

========================================================================================================================================================================
 Package                           Arch                                Version                                            Repository                               Size
========================================================================================================================================================================
Installing:
 tree                              x86_64                              1.6.0-10.amzn2.0.1                                 amzn2-core                               47 k

Transaction Summary
========================================================================================================================================================================
Install  1 Package

Total download size: 47 k
Installed size: 83 k
Is this ok [y/d/N]: y
Downloading packages:
tree-1.6.0-10.amzn2.0.1.x86_64.rpm                                                                                                               |  47 kB  00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : tree-1.6.0-10.amzn2.0.1.x86_64                                                                                                                       1/1
  Verifying  : tree-1.6.0-10.amzn2.0.1.x86_64                                                                                                                       1/1

Installed:
  tree.x86_64 0:1.6.0-10.amzn2.0.1

Complete!

コマンドの結果から自明ですが、バージョンです。

[root@ip-192-168-0-165 ~]# tree --version
tree v1.6.0 (c) 1996 - 2011 by Steve Baker, Thomas Moore, Francesc Rocher, Kyosuke Tokoro

これで事前の準備が整いました。

s3-tree のインストールおよび実行

早速s3-treepip3でインストールしました。

[root@ip-192-168-0-165 ~]# pip3 install s3-tree
WARNING: Running pip install with root privileges is generally not a good idea. Try `pip3 install --user` instead.
Collecting s3-tree
  Downloading https://files.pythonhosted.org/packages/a1/ae/07f9f825e4448016f4927d9d707e560d867519360aa984bc9e98c89e57e8/s3_tree-0.1.0-py3-none-any.whl
Requirement already satisfied: setuptools in /usr/lib/python3.7/site-packages (from s3-tree)
Installing collected packages: s3-tree
Successfully installed s3-tree-0.1.0

WARNING: Running pip install with root privileges is generally not a good idea. Try 'pip3 install --user' instead.という怒られが発生していますが、今回は検証なので気にしません。

インストールが成功したので help を確認してみます。ものすごくシンプルです。

[root@ip-192-168-0-165 ~]# s3-tree --help
usage: s3-tree [-h] [-v] [bucket]

list s3 bucket objects in a tree-like format.

positional arguments:
  bucket         list s3 bucket objects in a tree-like format.

                     $ s3-tree example

                     where example is the bucket you wanna show.

optional arguments:
  -h, --help     show this help message and exit
  -v, --version  show program's version number and exit

treeで使えるオプションがs3-treeでも使用できるわけではありません。階層の深さを指定したりフィリタリングを行ったりはできないので、全量を出力することになります。バケット内のオブジェクトが大量にある場合には注意が必要でしょう。

早速バケットを指定して実行してみます。

[root@ip-192-168-0-165 ~]# s3-tree <S3バケット名>
<S3バケット名>
├── fuga
│   ├── fuga1.txt
│   ├── fuga2.txt
│   └── fuga3.txt
├── hoge
│   ├── hoge1.txt
│   └── hoge2.txt
└── piyo
    └── kopiyo
        └── index.html

4 directories, 6 files

見慣れた tree の形式で結果が確認できました!

S3 バケットにおいては「ディレクトリ」「ファイル」という表現は正確ではありませんが、ディレクトリ数やファイル数をいつもの tree のノリで表示してくれます。

せっかくなので数十万オブジェクトがあるバケットに対して実行してみました。

[root@ip-192-168-0-165 ~]# s3-tree <オブジェクトいっぱいのS3バケット名>
<オブジェクトいっぱいのS3バケット名>
----略----
│                           ├── 000000000000_CloudTrail-Digest_us-west-2_testtrail_ap-northeast-2_20200901T055310Z.json.gz
│                           ├── 000000000000_CloudTrail-Digest_us-west-2_testtrail_ap-northeast-2_20200901T065310Z.json.gz
│                           ├── 000000000000_CloudTrail-Digest_us-west-2_testtrail_ap-northeast-2_20200901T075310Z.json.gz
│                           ├── 000000000000_CloudTrail-Digest_us-west-2_testtrail_ap-northeast-2_20200901T085310Z.json.gz
│                           ├── 000000000000_CloudTrail-Digest_us-west-2_testtrail_ap-northeast-2_20200901T095310Z.json.gz
│                           ├── 000000000000_CloudTrail-Digest_us-west-2_testtrail_ap-northeast-2_20200901T105310Z.json.gz
│                           └── 000000000000_CloudTrail-Digest_us-west-2_testtrail_ap-northeast-2_20200901T115310Z.json.gz
├── japanese_output
├── manifest.json
└── powershell-7.0.0-1.rhel.7.x86_64.rpm.zip

6466 directories, 259056 files

今回試した環境では、2分30秒程度で全ての出力が完了しました。

数十秒の間は画面に何も出力されなかったので、少しドキドキしました。

終わりに

S3 バケットの中身を tree 形式で表示してくれるツールs3-treeの確認でした。

細かいオプションには対応していないシンプル機能なので、ちょっとしたボリュームのバケットの中身をお手軽に確認したい、という場面で使うことになりそうです。

本当は、このようなツールを使わず AWS CLI の標準機能だけで、 tree 形式で表示できると嬉しいですね。ワンライナー的なやつでゴリゴリするとできたりするのでしょうか。詳しくないので分かりません。

冒頭の繰り返しとなりますが、公式のツールでないことを鑑み、適宜ご判断の上ご利用ください。

以上、千葉(幸)がお送りしました。