gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 01:15:58 26 4
gpt4 key购买 nike

我有一个亚马逊 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com