简体   繁体   中英

AWS Lambda to delete everything from a specific folder in an S3 bucket

I'm trying to delete everything from a specific folder in an S3 bucket with AWS Lambda using Python. The Lambda runs successfully however, the files still exist in "folder1". There will be no sub-folder under this folder except files.

Could someone please assist? Here is the code:

import json
import os
import boto3

def lambda_handler(event,context):
    s3 = boto3.resource('s3')
    deletefile_bucket = s3.Bucket('test_bucket')
    response = deletefile_bucket.delete_objects(
        Delete={
            'Objects': [
                {
                    'Key': 'folder1/'
                   
                },
            ],
        }
    )

The delete_objects() command requires a list of object keys to delete. It does not perform wildcard operations and it does not delete the contents of subdirectories.

You will need to obtain a listing of all objects and then specifically request those objects to be deleted.

The delete_objects() command accepts up to 1000 objects to delete.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM