简体   繁体   中英

Looping through a file to delete files from s3 - boto3?

This is probably very basic but I'm new to this so please bear with me!

I've been given a list of objects stored in s3 that need to be permanently deleted. This list is just a.txt file that I have no control over, but it does conveniently contain all of the specific keys that need to be removed eg:

folder1/file1.xyz 
folder1/file2.xyz
folder2/file1.xyz

This is what I am trying which does not seem to actually delete the objects:

import boto3
s3 = boto3.client('s3')
s3bucket = "my bucket"
trn = "list.txt"

with open(trn) as trn_list:
    for line in trn_list:
         s3.delete_object(Bucket=s3bucket, Key=line)

If I specify a key to delete instead of looping through the file, it works and the object is removed, for example:

s3.delete_object(Bucket=s3bucket, Key=folder1/file1.xyz)

but when I try to include the delete into the loop it never seems to delete anything. If I run the loop to just print(line) then it correctly prints out each key in the.txt file.

Is there a different way I need to be doing this? Any advice is appreciated.

When you're looping over each line, the line will contain the newline in the string, so you need to remove it before using it as the key to a call into boto3:

    for line in trn_list:
         s3.delete_object(Bucket=s3bucket, Key=line.strip())

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