Basically I have a PHP crud system. When I add a new php record, I would also like to make API post to bucket to create a new folder and somehow associate that new folder to that record. This way, when I search that record I can then list objects for that record from that specific folder. How do I create this folder?
-
1I'm not sure that you can create an empty "folder". According to [this post](https://stackoverflow.com/questions/38965266/how-to-create-a-folder-within-s3-bucket-using-php) and associated answers/comments, you "can't create an empty folder" but you can "create a 0-byte object with a '/' at the end of it's key". – showdev Jun 30 '19 at 00:57
-
1Possible duplicate of [How to create a folder within S3 bucket using PHP](https://stackoverflow.com/questions/38965266/how-to-create-a-folder-within-s3-bucket-using-php) – showdev Jun 30 '19 at 02:08
1 Answers
You don't need to create a folder.
Folders do not actually exist in Amazon S3. Instead, the filename (Key
) of an object contains the full path, including the filename of the object.
This means you could copy a file like this, even if the folders do not exist:
aws s3 cp foo.txt s3://my-bucket/one/two/foo.txt
To make things "appear" like a normal filesystem, the Amazon S3 management console will show one
and two
as folders, but they don't actually exist. In fact, if you to run this command:
aws s3 rm s3://my-bucket/one/two/foo.txt
This would not only delete the object, but the "folders" would also disappear, since they never existed in the first place.
The management console also makes it possible to "create" a folder, but it really just creates a zero-length object with the name (including full path) of the folder. This allows an "empty folder" to appear in the console (even though it doesn't exist).
So, if your application wishes to associate a "folder" with a record, it merely needs to associate the path with the record. There is on need to actually create the folder. Then, create files with that path as a prefix and they will appear to be in the folder but the reality is their filename (Key
) includes both the path and the filename.
Your application can call S3 and ask for a list of objects for a given prefix, and only the objects in that "folder" (prefix) will be returned.

- 241,921
- 22
- 380
- 470