php - 使用 php 快速列出 Amazon S3 存储桶中的所有文件的方法?

标签 php amazon-web-services amazon-s3 cdn

我有一个亚马逊 s3 存储桶,其中包含数万个文件名。获取所有文件或列出存储桶中所有文件名的文本文件的列表的最简单方法是什么?

我尝试过使用listObject(),但它似乎只列出了1000个文件。

amazon-s3-returns-only-1000-entries-for-one-bucket-and-all-for-another-bucket-u S3-Provider-does-not-get-more-than-1000-items-from-bucket

--> Listing Keys Using the AWS SDK for PHP 但在 aws 文档中我读到

max-keys - string - Optional - The maximum number of results returned by the method call. The returned list will contain no more results than the specified value, but may return fewer. The default value is 1000.

AWS DOC FOR list_objects

Is there some way to list it all and print it to a text file using AWS PHP SDK ?

可能重复: quick-way-to-list-all-files-in-amazon-s3-bucket

我重新发布了这个问题,因为我正在寻找 php 中的解决方案。

代码:

$s3Client = S3Client::factory(array('key' => $access, 'secret' => $secret));

$response = $s3Client->listObjects(array('Bucket' => $bucket, 'MaxKeys' => 1000, 'Prefix' => 'files/'));
$files = $response->getPath('Contents');
$request_id = array();
foreach ($files as $file) {
    $filename = $file['Key'];
    print "\n\nFilename:". $filename;

 }

最佳答案

要获取超过 1000 个对象,您必须使用 Marker 参数发出多个请求,以告诉 S3 每个请求的中断位置。使用Iterators适用于 PHP 的 AWS 开发工具包的功能使您可以更轻松地获取所有对象,因为它封装了发出多个 API 请求的逻辑。试试这个:

$objects = $s3Client->getListObjectsIterator(array(
    'Bucket' => $bucket,
    'Prefix' => 'files/'
));

foreach ($objects as $object) {
    echo $object['Key'] . "\n";
}

使用最新的 PHP SDK(截至 2016 年 3 月),代码必须按如下方式编写:

$objects = $s3Client->getIterator('ListObjects', array(
    'Bucket' => $bucket,
    'Prefix' => 'files/'
));

关于php - 使用 php 快速列出 Amazon S3 存储桶中的所有文件的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22165094/

相关文章:

php - 在 Wordpress 定制器中加入自定义脚本

amazon-web-services - Terraform EC2 实例导入 - 用户数据不同

amazon-web-services - AWS ECS Docker 容器 Boto3 IAM 权限

amazon-web-services - AWS将子域重定向到S3中的子文件夹

php - 如何在 PHP 的类中创建调度表?

php - 复杂的 Laravel 5 按集合分组

php - 获取mysql中最近的下一个日期记录

python - 博托 + Python + AWS S3 : How to get last_modified attribute of specific file?

amazon-s3 - 未使用 SinkMode.REPLACE 删除级联 S3 Sink Tap

javascript - 如何使用 Dropzone JS 获取 Amazon S3 响应 header ?