Reputation: 68
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 e.g:
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.
Upvotes: 0
Views: 1957
Reputation: 10827
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())
Upvotes: 2