Created
July 23, 2023 22:17
-
-
Save jrotenstein/4f2866736ae38f3b5272e2b4ec915aba to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
For https://stackoverflow.com/questions/76747790/%d0%90utomatic-deletion-files-from-s3: | |
Here's a sample Lambda function to do it: | |
```python | |
import boto3 | |
BUCKET_NAME = 'my-bucket-name' | |
def lambda_handler(event, context): | |
s3_resource = boto3.resource('s3') | |
for object in s3_resource.Bucket(BUCKET_NAME).objects.all(): | |
key = object.key | |
if key.startswith('logs/') or (key.endswith('.txt') and '/' not in key): | |
object.delete() | |
``` | |
You would need to: | |
- Create an IAM Role with sufficient S3 permissions to delete objects from your S3 bucket | |
- Create an AWS Lambda function (Python) with the above code, using the IAM Role | |
- Update the name of the bucket in the code | |
- Schedule the Lambda function to run every 24 hours |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment