How to delete S3 files starting with

Let's say I have images of different sizes on S3:

137ff24f-02c9-4656-9d77-5e761d76a273.webp
137ff24f-02c9-4656-9d77-5e761d76a273_500_300.webp
137ff24f-02c9-4656-9d77-5e761d76a273_400_280.webp

I use boto to delete a single file:

bucket = get_s3_bucket()
s3_key = Key(bucket)
s3_key.key = '137ff24f-02c9-4656-9d77-5e761d76a273.webp'
bucket.delete_key(s3_key)

But I would like to delete all keys starting with 137ff24f-02c9-4656-9d77-5e761d76a273.

Keep in mind that there can be hundreds of files in a bucket, so I don’t want to iterate over all the files. Is there a way to delete only files starting with a specific line?

Maybe some function to remove regular expressions.

+4
source share
3 answers

S3 multi-delete, 1000 API. API . , .

. , .

import boto

s3 = boto.connect_s3()
bucket = s3.get_bucket('mybucket')
to_delete = list(bucket.list(prefix='137ff24f-02c9-4656-9d77-5e761d76a273'))

list , list, to_delete , , .

1000 delete_keys bucket.

for chunk in [to_delete[i:i+1000] for i in range(0, len(to_delete), 1000)]:
    result = bucket.delete_keys(chunk)
    if result.errors:
        print('The following errors occurred')
        for error in result.errors:
            print(error)

(, ), , , - , .

+6

. s3cmd, S3. .

cmd = 's3cmd ls s3://bucket_name'
args = shlex.split(cmd)
ls_lines = subprocess.check_output(args).splitlines()

, ( regex, ). :

s3cmd del s3://bucket_name/file_name(s)

, :

s3cmd del s3://bucket_name/string*

, , , - .

+2

boto, , , , get_all_keys, , delete_keys.

, regex

+1

Source: https://habr.com/ru/post/1527717/


All Articles