Home > database >  Deleting a file that doesn't exist on S3 deletes the folder if that is the last file. How do we
Deleting a file that doesn't exist on S3 deletes the folder if that is the last file. How do we

Time:10-20

So, I have the following structure on S3:

mainbucket

  • DataFeeds/
  • Statement/

We had incidents where the DataFeeds/ folder was being deleted! So, I tested with following:

aws s3api put-object --bucket mainbucket --key DataFeeds/.donotdelete 

But, if I execute this (deleting blah.txt even if it does not exists) the DataFeeds/ folder gets deleted too:

aws s3 rm s3://mainbucket/DataFeeds/blah.txt

So, how do we prevent a folder from being deleted on S3?

Versions used: aws-cli/2.2.46 Python/3.9.7 Darwin/20.6.0 source/x86_64 prompt/off

CodePudding user response:

Folders do not exist in Amazon S3. It is a 'flat' storage service where the Key (filename) of an object includes the full path, including directories.

Amazon S3 will 'simulate' folders for you. For example, if you upload a file to invoices/january.txt, then the invoices directory will 'magically' appear. If you then delete that object, the directory will then 'disappear'. This is because it never actually existed.

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).

The best advice for using S3 is to pretend that folders exist. You can place an object in any path and the (pretend) directories will magically appear. Do not worry about directories disappearing, since they never actually existed!

If you really need empty directories to stay around, use that Create folder button to create the zero-length object. It will stay around until you delete the zero-length object.

  • Related