I’m working on an auto-update solution, and I’m using Amazon S3 for distribution.
I would like for this to work like follows:
- I upload a file to s3 folder
- An automatic PHP script detects that a new file has been added and notifies clients
To do this, I somehow need to list all files in an amazon bucket’s folder, and find the one which has been added last.
I’ve tried $s3->list_objects("mybucket");, but it returns the list of all objects inside the bucket, and I don’t see an option to list only files inside the specified folder.
What is the best way to do this using Amazon S3 PHP api?
S3’s API isn’t really optimized for sort-by-modified-date, so you’d need to call
list_buckets()and check each timestamp, always keeping track of the newest one until you get to the end of the list.You’d need to write a long-running PHP CLI script that starts with:
Maybe throw an occasional
sleep(1)in there so that your CPU doesn’t spike so badly, but you essentially need to sleep-and-poll, looping over all of the timestamps each time.You’ll want to set the
prefixparameter in yourlist_objects()call.