Amazon S3 has a flat structure with no real folders or directories. The folder-like appearance in the S3 console is an illusion created by using key name prefixes with the '/' delimiter. When you delete an object whose key name ends with '/', it appears as if the folder is being deleted, but in reality, there is no separate folder entity in S3.
To preserve the appearance of folders in the S3 console, you need to leave at least one object with the desired prefix in the bucket.
For example, if you want to keep the folder 'myfolder/', you should leave an object like 'myfolder/placeholder.txt' in the bucket.
In S3, everything is Objects. This means that you can upload objects with trailing slash "/" by using AWS CLI or AWS SDK and an object named with a trailing "/" displays as a folder in the Amazon S3 console.
For example, if you upload a file to s3://bucketname/folder/file.txt
, then the folder
directory will appear. If the object 'file.txt' is deleted, the directory will also be deleted. This is because If you use the Create folder button in the S3 management console, it will create a zero-length object with the same name as the directory. This will 'force' the directory to appear in the bucket listing. Deleting the zero-length object will cause the directory to disappear if there are no objects with that same prefix (path). I have drafted another example shown below:
s3://bucketname/prefixA/prefixAA/test.txt
s3://bucketname/prefixA/object.txt
s3://bucketname/prefixB/photo.png
s3://bucketname/prefixB/photo2.png
Deleting the object " test.txt ", the S3 bucket will then look like this:
s3://bucketname/prefixA/object.txt
s3://bucketname/prefixB/photo.png
s3://bucketname/prefixB/photo2.png
The key path " s3://bucketname/prefixA/prefixAA/ " has been removed because there are no other objects present in the folder or prefix " s3://bucketname/prefixA/prefixAA/ ".
Since deleting the object 'test.txt', there are no longer any objects present in the key path s3://bucketname/prefixA/prefixAA/
. All the folders/prefixes are deleted up until prefixA
and this prefix is kept because it has other objects present in it. Thus, to retain these folders/prefixes from being deleted, please always have at least one object present in it.
However, as a workaround, you can create a 0 byte object of folder type either from the console "Create folder" option or using CLI as below:
aws s3api put-object --bucket my-bucket --key folder/
The prefix will not be deleted as there will be a 0 byte file existing and not deleted in the prefix.