In the S3 REST API, when iterating through objects, you often specify a key prefix, which is a left-anchored substring matching all the key values you want returned.
When you tell S3 you want foo/
, what you are, of course, asking for is foo/*
.
Perhaps less intuitive is the fact that asking for foo
is really asking for foo*
, which would include foo*/*
.
It's a prefix match. Any key with a matching prefix will be included, so the prefix foo
would include not only foo/*
but also foobar/*
, etc.
This is why some of us are so seemingly fond of issuing the friendly reminder that "S3 is not a filesystem, it is an object store," even though at some level, you already knew that. It doesn't precisely follow filesystem semantics. This, I would suggest, is one of the reasons the sometimes subtle-seeming distinctions are important.
Unlike a filesystem, the directory hierarchy in S3 is not really there. It's a convenient illusion based on the /
character. The folders you can create in the console are similarly an illusion -- they're empty objects the console lets you add in order to create the appearance of a hierarchy before you actually have any keys with that prefix in the bucket. So, there is no concept of objects actually being "in" folders, they're just "under" folders.
Without the trailing slash, I suspect you are matching more than you anticipate, because of the prefix-matching paradigm.