PHP:选择添加到Amazon S3文件夹的最新文件 [英] PHP: select the latest file added to an Amazon S3 folder

查看:72
本文介绍了PHP:选择添加到Amazon S3文件夹的最新文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一种自动更新解决方案,并且正在使用Amazon S3进行分发.

I'm working on an auto-update solution, and I'm using Amazon S3 for distribution.

我希望此操作如下:

  1. 我将文件上传到s3文件夹
  2. 自动PHP脚本检测到已添加新文件并通知客户端

为此,我需要以某种方式列出亚马逊存储桶文件夹中的所有文件,并找到最后添加的文件.

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.

我尝试了 $ s3-> list_objects("mybucket"); ,但是它返回了存储桶中所有对象的列表,但我看不到仅列出选项指定文件夹中的文件.

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.

使用Amazon S3 PHP api做到这一点的最佳方法是什么?

What is the best way to do this using Amazon S3 PHP api?

推荐答案

为此,我需要以某种方式列出亚马逊存储桶文件夹中的所有文件,并找到最后添加的文件.

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.

S3的API并未真正针对按修改日期排序进行优化,因此您需要调用 list_buckets()并检查每个时间戳,始终跟踪最新时间戳,直到您找到为止到达列表的末尾.

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.

自动PHP脚本检测到已添加新文件并通知客户端

An automatic PHP script detects that a new file has been added and notifies clients

您需要编写一个以以下内容开头的长期运行的PHP CLI脚本:

You'd need to write a long-running PHP CLI script that starts with:

while (true) { /*...*/ }

也许偶尔在其中扔 sleep(1),以使您的CPU不会出现严重的尖峰,但实际上您需要进行睡眠和轮询,每次都遍历所有时间戳

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.

我已经尝试过$ s3-> list_objects("mybucket");,但是它返回了存储桶中所有对象的列表,但我看不到仅列出指定文件夹中文件的选项.

您将要在

You'll want to set the prefix parameter in your list_objects() call.

这篇关于PHP:选择添加到Amazon S3文件夹的最新文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆