日本語

How to List S3 Objects by Folder in AWS Emulator

​​AWS Emulator​Amazon S3,AWS CLI

Adding --delimiter / to S3's list-objects-v2 returns only the files directly inside that folder and the names of the folders below it. Adding --prefix photos/ lets you go inside photos/ and list it the same way. This combination is useful when you want to step through S3 folders one level at a time, as in a file browser, or view logs grouped by date folder. I did not use an AWS account. Instead, I ran AWS Emulator locally and checked the behavior with the commands and output shown as they are.

Note

The test environment was Ubuntu 24.04 (x86_64), AWS Emulator 1.0.624 (released November 26, 2026), and AWS CLI 2.42.4. I did not use an AWS account or credentials. I did not test the behavior on real Amazon S3, the S3 console display, directory buckets, the older list-objects (ListObjects) API, or calls from an SDK. Where I describe real S3 behavior, the source is the official documentation. Also, AWS checks the emulator's behavior against the real service only in us-west-2, but this article was tested in us-east-1.

S3 has no folders, only keys in a flat list

As the official documentation on folders in S3 explains, a general purpose bucket has a flat structure with no hierarchy. photos/2025/cat1.jpg is not "a file inside the 2025 folder inside the photos folder." It is a single name (a key) that contains slashes. The documentation explains that the console shows it like a folder because it groups keys by their shared beginning (the prefix).

The two parameters that do this grouping in the listing API are prefix and delimiter. According to the official documentation on prefixes, prefix narrows the results to keys that begin with the given string, and delimiter groups keys that share a common part into a single result.

Putting files in place to test with

Installing the emulator and setting the environment variables are the same as in the separate article, "How to install AWS Emulator on Ubuntu and start using it." I started it with aws-emulator -t off and pointed the AWS CLI at the emulator. (-t off tells it not to send usage data to AWS.)

I created a bucket and placed 10 keys, each holding a small file containing only hello.

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

If I list without any options, the 10 keys come back in one list, sorted by name. There are no folder separators.

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"
]

Listing only the top level of a bucket with delimiter

Adding --delimiter / changes how the results come back. Keys without a slash go into Contents. Keys with a slash are grouped up to the first slash and placed in CommonPrefixes. (In Contents, I omitted lines such as ETag from each item.)

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 holds the files at that level, and CommonPrefixes holds the folders below it. The ListObjectsV2 reference says that CommonPrefixes is returned only when a delimiter is specified, and that the grouped keys do not appear anywhere else in the response. From here on, I use --query to pull out the two fields so they are easier to read. Files and Folders are names I chose.

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/"
    ]
}

Adding prefix to go inside a folder

To see what is inside photos/, I add --prefix photos/. Everything after photos/ is grouped up to the first slash, so 2025/ and 2026/ come back as folders. photos/index.html has no further slash, so it comes back as a file.

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/"
    ]
}
Keys that share the same text after photos/ up to the first slash are grouped in CommonPrefixes, photos/index.html goes into Contents, and photos.zip is excluded
How keys are sorted with prefix=photos/ and delimiter=/

Passing a value from CommonPrefixes as the next --prefix takes you one level deeper. There are no folders under photos/2025/, so Folders is 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
}

If I use only --prefix photos/ without --delimiter, nothing is grouped, and all files under the folder come back, including those in deeper levels. Use this form when you want to process the entire contents of a folder.

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
}

Specifying a folder that does not exist does not cause an error. The response has neither Contents nor CommonPrefixes.

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

Forgetting the trailing slash in prefix also matches files that start with the same name

prefix filters for keys that "begin with this string." It does not treat the value as a folder name. If I use --prefix photos without the trailing slash, photos.zip also matches. The only folder returned is photos/, and I did not go inside it.

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

When you list the contents of a folder, end --prefix with a slash, as in photos/. The values returned in CommonPrefixes end with a slash, so you can pass them directly as the next --prefix.

The delimiter does not have to be a slash

The official documentation explains that a slash is only commonly used, not a special character, and that other characters can also be used as a delimiter. When I used - as the delimiter inside logs/, the three keys were grouped into logs/2026-, which runs from after logs/ up to the first -.

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

An empty folder is a 0-byte file ending in a slash

Because S3 only has a list of keys, a folder that contains no files does not appear in the listing by default. According to the official documentation on folders in S3, when you create a folder in the console, S3 creates a 0-byte object whose key ends in a slash. With the AWS CLI, you can do the same by running put-object without --body.

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

empty-folder/ now appears as a folder in the top-level listing.

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/"
    ]
}

When I go inside this folder, empty-folder/, which is the folder object itself, comes back as a file of size 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
    }
]

When I list this folder with aws s3 ls, one row with no name and a size of 0 appears. This row is the item in Contents whose key matches the --prefix value, which is the folder object itself.

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

Conversely, a folder without a 0-byte object disappears from the listing when its files are deleted. When I deleted docs/guide.md, which was inside docs/, docs/ disappeared from the top-level listing.

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/"
    ]
}

Folders also count as one item per response page

By default, a single response returns up to 1,000 items. According to the reference, each group of keys in CommonPrefixes counts as one item, no matter how many keys it contains. When I called it once with --max-keys 2 (--no-paginate tells the AWS CLI not to fetch the rest automatically), two folders made up the two items. The response included IsTruncated, which indicates that more results exist, and NextContinuationToken, which is used to fetch them. (I omitted the rest of the token.)

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…(省略)"
}

When I passed this token to --continuation-token, one more file and one more folder came back. Files and folders together are split into groups of two in name order. (. sorts before /, so photos.zip comes before 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…(省略)"
}

There is one thing to watch for here. In the AWS CLI in my test environment, even without --no-paginate, specifying --max-keys 2 displayed only the first 2 items. The --debug output showed only one request. If you want to fetch everything while making each request smaller, use --page-size, described in the AWS CLI pagination documentation. With --page-size 2, the CLI sent three requests and displayed all the results together.

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 shows the same listing with PRE lines

In the --debug output in my test environment, aws s3 ls also called the same listing API with delimiter=/. When I listed s3://example-bucket/photos/, the request sent was prefix=photos%2F&delimiter=%2F. CommonPrefixes is displayed as PRE lines.

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

aws s3 ls also changes its results depending on whether the path ends with a slash. Without the slash, the photos/ folder and photos.zip are listed together.

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

To see all the files inside without stepping through folders, add --recursive, as described in the aws s3 ls reference. If you want to process each level in a script, passing --prefix and --delimiter to list-objects-v2, which returns JSON, lets you receive Contents and CommonPrefixes separately.

​Posted by 86