English

AWS EmulatorでS3をフォルダーのように一覧する方法

​​AWS Emulator​Amazon S3,AWS CLI

S3 の list-objects-v2 に --delimiter / を付けると、そのフォルダーの直下のファイルと、その下のフォルダーの名前だけが返ります。--prefix photos/ を足せば、photos/ の中に入って同じように一覧できます。ファイルブラウザのような画面で S3 のフォルダーを1階層ずつたどりたいときや、ログを日付のフォルダーごとに見たいときに使う組み合わせです。AWS のアカウントは使わず、手元で動かした AWS Emulator を相手に、コマンドと出力をそのまま載せて確かめます。

補足

検証環境は Ubuntu 24.04(x86_64)、AWS Emulator 1.0.624(2026年11月26日リリース)、AWS CLI 2.42.4 です。AWS のアカウントと認証情報は使っていません。本物の Amazon S3 での動き、S3 のコンソールの表示、ディレクトリバケット、古い list-objects(ListObjects)、SDK からの呼び出しは確かめていません。本物の S3 について書いたところは、公式ドキュメントを出どころにしています。また、AWS がエミュレーターの動きを本物と照らし合わせているのは us-west-2 だけですが、この記事は us-east-1 で試しています。

S3 にフォルダーはなく、キーが並んでいるだけ

S3 のフォルダーについての公式ドキュメントにあるとおり、汎用バケットの中は階層のない平らな作りです。photos/2025/cat1.jpg は「photos フォルダーの中の 2025 フォルダーの中のファイル」ではなく、スラッシュを含む1つの名前(キー)です。コンソールがフォルダーのように見せているのは、キーの頭の共通部分(プレフィックス)でまとめているからだと説明されています。

一覧の API でこのまとめ方をするのが、prefix と delimiter の2つのパラメーターです。プレフィックスについての公式ドキュメントによると、prefix はキーの頭が合うものだけに絞り、delimiter は共通の部分を持つキーを1つの結果にまとめます。

試すためのファイルを置く

エミュレーターの入れ方と環境変数の設定は、別の記事「AWS EmulatorをUbuntuに入れて使い始める方法」と同じです。aws-emulator -t off で起動し、AWS CLI の接続先をエミュレーターに向けました(-t off は利用状況のデータを AWS に送らない指定です)。

バケットを作り、中身が hello だけの小さなファイルを、10個のキーで置きます。

text
$ aws s3api create-bucket --bucket example-bucket
{
    "Location": "/example-bucket",
    "BucketArn": "arn:aws:s3:::example-bucket"
}
$ echo hello > f.txt; for k in readme.txt photos.zip photos/index.html photos/2025/cat1.jpg photos/2025/cat2.jpg photos/2026/cat3.jpg docs/guide.md logs/2026-10-31.log logs/2026-11-01.log logs/2026-11-02.log; do aws s3api put-object --bucket example-bucket --key $k --body f.txt >/dev/null && echo put $k; done
put readme.txt
put photos.zip
(途中は省略)
put logs/2026-11-02.log

何も付けずに一覧すると、10個のキーが名前の順に1列で返ります。フォルダーの区切りはありません。

text
$ aws s3api list-objects-v2 --bucket example-bucket --query 'Contents[].Key'
[
    "docs/guide.md",
    "logs/2026-10-31.log",
    "logs/2026-11-01.log",
    "logs/2026-11-02.log",
    "photos.zip",
    "photos/2025/cat1.jpg",
    "photos/2025/cat2.jpg",
    "photos/2026/cat3.jpg",
    "photos/index.html",
    "readme.txt"
]

delimiter でバケットの直下だけを一覧する

--delimiter / を付けると、返り方が変わります。スラッシュを含まないキーは Contents に、スラッシュを含むキーは最初のスラッシュまでの部分にまとめられて CommonPrefixes に入りました(Contents の各項目は、ETag などの行を省略しています)。

text
$ aws s3api list-objects-v2 --bucket example-bucket --delimiter /
{
    "Contents": [
        {
            "Key": "photos.zip",
            "LastModified": "2026-11-26T05:43:14+00:00",
            (省略)
        },
        {
            "Key": "readme.txt",
            "LastModified": "2026-11-26T05:43:14+00:00",
            (省略)
        }
    ],
    "CommonPrefixes": [
        {
            "Prefix": "docs/"
        },
        {
            "Prefix": "logs/"
        },
        {
            "Prefix": "photos/"
        }
    ],
    "RequestCharged": null,
    "Prefix": ""
}

Contents がその階層のファイル、CommonPrefixes がその下のフォルダーにあたります。ListObjectsV2 のリファレンスでは、CommonPrefixes は delimiter を指定したときだけ返り、まとめられたキーは応答のほかの場所には出ないと書かれています。ここからは、見やすいように --query で2つを取り出して表示します。Files と Folders は、私が付けた名前です。

text
$ aws s3api list-objects-v2 --bucket example-bucket --delimiter / --query '{Files: Contents[].Key, Folders: CommonPrefixes[].Prefix}'
{
    "Files": [
        "photos.zip",
        "readme.txt"
    ],
    "Folders": [
        "docs/",
        "logs/",
        "photos/"
    ]
}

prefix を足して、フォルダーの中に入る

photos/ の中を見るには、--prefix photos/ を足します。photos/ のあとで最初に出てくるスラッシュまでがまとめられ、2025/ と 2026/ がフォルダーとして返りました。スラッシュがもう出てこない photos/index.html は、ファイルとして返ります。

text
$ aws s3api list-objects-v2 --bucket example-bucket --prefix photos/ --delimiter / --query '{Files: Contents[].Key, Folders: CommonPrefixes[].Prefix}'
{
    "Files": [
        "photos/index.html"
    ],
    "Folders": [
        "photos/2025/",
        "photos/2026/"
    ]
}
キーのうち photos/ のあとで最初のスラッシュまでが同じものが CommonPrefixes にまとまり、photos/index.html は Contents に入り、photos.zip は対象外になる
prefix=photos/ と delimiter=/ で、キーがどう振り分けられるか

返ってきた CommonPrefixes の値を次の --prefix に渡せば、もう1階層下に入れます。photos/2025/ の下にフォルダーはないので、Folders は null になりました。

text
$ aws s3api list-objects-v2 --bucket example-bucket --prefix photos/2025/ --delimiter / --query '{Files: Contents[].Key, Folders: CommonPrefixes[].Prefix}'
{
    "Files": [
        "photos/2025/cat1.jpg",
        "photos/2025/cat2.jpg"
    ],
    "Folders": null
}

--delimiter を付けずに --prefix photos/ だけにすると、まとめられずに、下の階層のファイルまで全部が返りました。フォルダーの中身をまるごと処理したいときは、こちらを使います。

text
$ aws s3api list-objects-v2 --bucket example-bucket --prefix photos/ --query '{Files: Contents[].Key, Folders: CommonPrefixes[].Prefix}'
{
    "Files": [
        "photos/2025/cat1.jpg",
        "photos/2025/cat2.jpg",
        "photos/2026/cat3.jpg",
        "photos/index.html"
    ],
    "Folders": null
}

存在しないフォルダーを指定しても、エラーにはなりません。Contents も CommonPrefixes もない応答が返りました。

text
$ aws s3api list-objects-v2 --bucket example-bucket --prefix nothing/ --delimiter /
{
    "RequestCharged": null,
    "Prefix": "nothing/"
}

prefix の末尾のスラッシュを忘れると、同じ名前で始まるファイルも入る

prefix は「この文字列で始まるキー」という絞り込みで、フォルダーの名前として扱われるわけではありません。末尾のスラッシュを付けずに --prefix photos とすると、photos.zip も合ってしまいます。フォルダーとしては photos/ が1つ返るだけで、中には入れていません。

text
$ aws s3api list-objects-v2 --bucket example-bucket --prefix photos --delimiter / --query '{Files: Contents[].Key, Folders: CommonPrefixes[].Prefix}'
{
    "Files": [
        "photos.zip"
    ],
    "Folders": [
        "photos/"
    ]
}
ポイント

フォルダーの中を一覧するときは、--prefix を photos/ のようにスラッシュで終えます。CommonPrefixes で返ってきた値は末尾がスラッシュなので、そのまま次の --prefix に渡せます。

delimiter はスラッシュでなくてもよい

公式ドキュメントでは、スラッシュは一般的に使われるだけで特別な文字ではなく、ほかの文字も delimiter にできると説明されています。logs/ の中を - で区切ってみると、logs/ のあとで最初の - までの logs/2026- に3つのキーがまとまりました。

text
$ aws s3api list-objects-v2 --bucket example-bucket --prefix logs/ --delimiter - --query '{Files: Contents[].Key, Folders: CommonPrefixes[].Prefix}'
{
    "Files": null,
    "Folders": [
        "logs/2026-"
    ]
}

空のフォルダーは、スラッシュで終わる0バイトのファイル

キーが並んでいるだけなので、ファイルが1つもないフォルダーは、そのままでは一覧に出ません。S3 のフォルダーについての公式ドキュメントによると、コンソールで「フォルダーの作成」をしたときは、名前にスラッシュを付けたキーで、0バイトのオブジェクトが作られます。AWS CLI では、put-object に --body を付けずに置けます。

text
$ aws s3api put-object --bucket example-bucket --key empty-folder/
{
    "ETag": "\"d41d8cd98f00b204e9800998ecf8427e\"",
    "ChecksumCRC64NVME": "AAAAAAAAAAA=",
    "ChecksumType": "FULL_OBJECT",
    "ServerSideEncryption": "AES256"
}

直下の一覧に、empty-folder/ がフォルダーとして加わりました。

text
$ aws s3api list-objects-v2 --bucket example-bucket --delimiter / --query '{Files: Contents[].Key, Folders: CommonPrefixes[].Prefix}'
{
    "Files": [
        "photos.zip",
        "readme.txt"
    ],
    "Folders": [
        "docs/",
        "empty-folder/",
        "logs/",
        "photos/"
    ]
}

このフォルダーの中に入ると、フォルダーそのものである empty-folder/ が、大きさ0のファイルとして返ります。

text
$ aws s3api list-objects-v2 --bucket example-bucket --prefix empty-folder/ --delimiter / --query 'Contents[].{Key: Key, Size: Size}'
[
    {
        "Key": "empty-folder/",
        "Size": 0
    }
]

aws s3 ls でこのフォルダーを一覧すると、名前のない大きさ0の行が1つ出ました。この行は、list-objects-v2 の Contents で --prefix と同じキーを持つ項目、つまりフォルダーそのものです。

text
$ aws s3 ls s3://example-bucket/empty-folder/
2026-11-26 05:44:04          0 

逆に、0バイトのオブジェクトがないフォルダーは、中のファイルを消すと一覧から消えます。docs/ の中にあった docs/guide.md を消すと、直下の一覧から docs/ がなくなりました。

text
$ aws s3api delete-object --bucket example-bucket --key docs/guide.md
$ aws s3api list-objects-v2 --bucket example-bucket --delimiter / --query '{Files: Contents[].Key, Folders: CommonPrefixes[].Prefix}'
{
    "Files": [
        "photos.zip",
        "readme.txt"
    ],
    "Folders": [
        "empty-folder/",
        "logs/",
        "photos/"
    ]
}

1回の応答の件数では、フォルダーも1件と数える

1回の応答で返るのは、既定で1,000件までです。リファレンスによると、CommonPrefixes にまとめられたキーは、いくつあっても1件として数えられます。--max-keys 2 で1回だけ呼ぶと(--no-paginate は、AWS CLI に続きを自動で取りに行かせない指定です)、フォルダー2つで2件になり、続きがあることを示す IsTruncated と、続きを取るための NextContinuationToken が返りました(トークンの後ろは省略しています)。

text
$ aws s3api list-objects-v2 --bucket example-bucket --delimiter / --max-keys 2 --no-paginate --query '{Files: Contents[].Key, Folders: CommonPrefixes[].Prefix, KeyCount: KeyCount, IsTruncated: IsTruncated, Next: NextContinuationToken}'
{
    "Files": null,
    "Folders": [
        "docs/",
        "logs/"
    ],
    "KeyCount": 2,
    "IsTruncated": true,
    "Next": "qy3QZWy6H8ZQizUy9iTi…(省略)"
}

このトークンを --continuation-token に渡すと、続きのファイル1つとフォルダー1つが返りました。ファイルとフォルダーを合わせて、名前の順に2件ずつ区切られています(. は / より前に並ぶので、photos.zip が photos/ より先です)。

text
$ T=$(aws s3api list-objects-v2 --bucket example-bucket --delimiter / --max-keys 2 --no-paginate --query NextContinuationToken --output text); aws s3api list-objects-v2 --bucket example-bucket --delimiter / --max-keys 2 --no-paginate --continuation-token "$T" --query '{Files: Contents[].Key, Folders: CommonPrefixes[].Prefix, KeyCount: KeyCount, IsTruncated: IsTruncated, Next: NextContinuationToken}'
{
    "Files": [
        "photos.zip"
    ],
    "Folders": [
        "photos/"
    ],
    "KeyCount": 2,
    "IsTruncated": true,
    "Next": "hLMF5aEdgdvRkuBToihv…(省略)"
}

ここで1つ気をつけることがあります。検証環境の AWS CLI では、--no-paginate を付けなくても、--max-keys 2 を指定すると最初の2件しか表示されませんでした。--debug の出力では、リクエストは1回だけです。全部を取りながら1回の件数だけを小さくしたいときは、AWS CLI のページ分割のドキュメントにある --page-size を使います。--page-size 2 では、リクエストを3回に分けて送り、全部をまとめて表示しました。

text
$ aws s3api list-objects-v2 --bucket example-bucket --delimiter / --page-size 2 --query '{Files: Contents[].Key, Folders: CommonPrefixes[].Prefix}'
{
    "Files": [
        "photos.zip",
        "readme.txt"
    ],
    "Folders": [
        "docs/",
        "logs/",
        "photos/"
    ]
}

aws s3 ls は、同じ一覧を PRE の行で表示する

検証環境で --debug を見ると、AWS CLI の aws s3 ls も同じ一覧の API を delimiter=/ で呼んでいました。s3://example-bucket/photos/ の一覧で送ったのは、prefix=photos%2F&delimiter=%2F のリクエストです。CommonPrefixes は PRE の行として表示されます。

text
$ aws s3 ls s3://example-bucket/photos/
                           PRE 2025/
                           PRE 2026/
2026-11-26 05:43:15          6 index.html

aws s3 ls でも、末尾のスラッシュの有無で結果が変わります。スラッシュを付けないと、photos/ のフォルダーと photos.zip が並びました。

text
$ aws s3 ls s3://example-bucket/photos
                           PRE photos/
2026-11-26 05:43:14          6 photos.zip

フォルダーをたどらずに中のファイルを全部見たいときは、aws s3 ls のリファレンスにあるとおり、--recursive を付けます。スクリプトで階層ごとに処理するなら、JSON で返る list-objects-v2 に --prefix と --delimiter を付けるほうが、Contents と CommonPrefixes を分けて受け取れます。

​Posted by 86