AWS configuration to delete files - amazon-web-services

I have a folder "Execution" folder in s3 bucket.
It has folders and files like
Execution
Exec_06-06-2022/
file1.json
file2.json
Exec_07-06-2022/
file3.json
file4.json
I need to configure to delete the Exec_datestamp folders and the inside files after X days.
I tried this using AWS lifecycle config for the prefix "Execution/"
But it deletes the folder Execution/ after X days ( set this to 1 day to test)
Is there any other way to achieve this?

There are no folders in S3. Execution is part of objects name, specifically its key prefix. The S3 console only makes Execution to appear as a folder, but there is no such thing in S3. So your lifecycle deletes Execution/ object (not folder), because it matches your query.
You can try with Execution/Exec* filter.

Got it, https://stackoverflow.com/a/41459761/5010582. The prefix has to be "Execution/Exec" without any wildcard.

Related

AWS S3 - Use powershell to delete all files but keep the folders

I have a powershell script, that downloads all files form an S3 bucket, and then removes the files from the bucket. All the files I'm removing are stored in a subfolder in the S3 bucket, and I just want to delete the files but maintain the subfolders.
I'm currently using the following command to delete the files in S3 once the file has been downloaded from S3.
Remove-S3Object -BucketName $S3Bucket -Key $key -Force
My problem is that if it removes all the files in the subfolder, the subfolder is removed as well. Is there a way to remove the file, but keep the subfolder present using powerhsell. I believe I can do something like this,
aws s3 rm s3://<key_to_be_removed> --exclude "<subfolder_key>"
but not quite sure if that'll work.
I'm looking for the best way to accomplish this, and at the moment, my only option is to recreate the subfolder via the script if the subfolder not longer exists.
The only way to accomplish having an empty folder is to create a zero-length object which has the same name as the folder you want to keep. This is actually how the S3 console enables you to create an empty folder.
You can check this by running $ aws s3 ls s3://your-bucket/folderfoo/ and observing an output object having length of zero bytes.
See more on this topic here.
As already commented, S3 does not really have folders the way file systems do. The folders as presented by most S3 browsers are just generated based on the paths of the files/objects. If you upload an object/file named folder/file, the browsers will present folder as folder with file as a file in the folder. But technically, all that there is is the file/object folder/file. The folder does not exist on its own.
You can explicitly create a folder by creating an empty empty-named object with "in the folder": folder/. If you do that, it will appear the the folder exists even if there are no files in it. But if you do not do that, the virtual folder disappears once you remove all objects in the folder.
Now the question is whether your command removes even the empty named object representing the folder or not. I cannot tell that.

Delete files inside a subfolder residing inside a bucket on amazon s3

On my s3 I have a bucket called nba-dataset. Inside the nba-dataset i have a folder called env. Inside the folder env i have another folder called prod-stage.
So basically The path looks like this nba-dataset > env > prod-stage.
I have a files inside prod-stage that I want to delete after x number of days.
So the way i understand is I can apply a lifecycle rule on the bucket nba-dataset.
The confusing part for me is what should the value be for prefix. Would it be env/prod-stage or would it be simply prod-stage?
I would appreciate guidance here as there are many files in the subfolders of the bucket nba-dataset that I don't want to delete by accident. I only want to delete files inside the prod-stage folder that are older than x days.
The prefix is an absolute path so in your case it is env/prod-stage/.
There is no such thing as a subfolder within S3, each object has a key which the GUI displays as subfolders by splitting the prefix separately by the "/"character.
In fact the interface is simply calling the list-objects method. When you're displayed as being in the env folder the prefix is env, then when you move into the subfolder of prod-stage the prefix becomes env/prod-stage.

How to prevent Amazon S3 object expiration from deleting the directory

We want to delete temp files from the S3 bucket from one of the folder on daily basis. I have tried with s3 lifecycle policy. For example my folder name is Test, I have set prefix for expiration is Test/ . But the issue here is along with all the files, Test folder is also getting deleted. I want to keep the folder as is and only delete the files in that . Is there any way i can do this?
Don't worry about the folder.
As soon as you have at least one file "in" the folder (object key beginning with Test/) it will appear again and when there are no objects beginning with that key prefix it will disappear -- all of this is normal/expected behavior because folders in S3 are not containers like they are on a filesystem and they do not actually need to exist before putting files "in" them.

Folders in S3 Bucket not visible in Web Console

After deleting a few folders in our S3 bucket, I am not able to see any of my folders through the web console. We had around 10 folders and ended up deleting 6 of them. The remaining four show up when I do an 'ls' on that S3 bucket through the CLI but the bucket shows up empty on the web console.
When I turn on 'Versions' I see everything (including the 6 folders that were deleted). Am I overlooking something extremely simple?
Folders do not actually exist in Amazon S3.
For example, you could create an object like this:
aws s3 cp foo.txt s3://my-bucket/folder1/folder2/foo.txt
This would instantly 'create' folder1 and folder2. Or, to be more accurate, the folders would 'appear' but they don't actually exist because the full filename (Key) of the object is folder1/folder2/foo.txt.
If you were then to delete that object, the folders would 'disappear' because they never actually existed.
Sometimes, if a system wants to forcefully make a folder 'appear', it can create a zero-length object with the same name as the folder. This makes the folder 'appear', but it is really the empty file that is appearing.
Bottom line: Don't worry about creating and deleting folders. They will appear when necessary and disappear when not being used. Do not try to map normal filesystem behaviour to Amazon S3.

delete files from a particular folder automatically in aws s3 bucket

I want to delete files from s3 bucket. Inside test bucket, there is a folder named mi and inside mi archive.
I configured life cycle rule on test bucket to delete file abc.txt from test/mi/archive/abc.txt after 7 days. I want to delete only abc.txt but it deletes full archive folder not only file.
At the time of rule apply on test bucket, I gave prefix mi/archive/.
S3 doesn't have folders, only object key prefixes. If there is no object with mi/archive in the prefix then that "folder" is not going to appear.
This really shouldn't be an issue. The next time you upload an object with mi/archive prefix in the key the "folder" will appear again.
Thanks to all for giving suggestion....
Finally, I got a solution. I did some changes in prefix. In place of "mi/archive", i gave files starting letters because my all files starts with "cd". Suppose there is a file named "cd_abcd.txt". So at the time of rule configuration on "test" bucket, i putted prefix "mi/archive/cd". So after 7 days, only files will be delete not full "archive" folder.
lifecycling is only for the entire folder/bucket. Your best/cheapest bet is probably a scheduled lambda to check for the file, it's creation date, and delete if necessary.