简体   繁体   中英

Delete versioned files in Amazon S3 using PHP SDK

I am tasked with cleaning up unreferenced images on our S3 bucket and found out we have versioning turned on.

We are currently using the PHP DSK (version '2006-03-01'), which, according to the documentation , does not seem to directly support deleting all versions of a particular file.

My question is this:

What is the right way to go about deleting versioned files in an S3 bucket using the methods available to me in the AWS PHP SDK?

Potentially, I was thinking of using listObjectVersions() and then running the delete on each one, but I'm not 100% that would work.

Any help is appreciated.

As you mentioned, you'll need to get a list of all the object versions and figure out which ones you want to delete. Then, instead of deleting each version one-by-one you can delete multiple versions at once (up to 1000) using a single API call.

See Delete Multiple Objects and Deleting Multiple Objects Using the AWS SDK for PHP for more details.

Do not try to mix the listing & deleting operations (list 1000 versions, remove 1000, list again, remove again, ...) as you'll end up with weird results because of S3's eventual consistency model.

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