我有一个亚马逊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 docs中我读了
max-keys - string - 可选 - 返回的最大结果数 通过方法调用。返回的列表将不再包含结果 比指定的值,但可能会返回更少。默认值为 1000。
有没有办法列出所有内容并使用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;
}
答案 0 :(得分:13)
要获得超过1000个对象,您必须使用Marker
参数发出多个请求,以告知S3您为每个请求停止的位置。使用AWS SDK for PHP的Iterators功能可以更轻松地获取所有对象,因为它封装了发出多个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/'
));
答案 1 :(得分:1)
下面的代码只是一个技巧,解决这个问题,我已经指向我的 CDN bucket
文件夹,它按字母顺序排列了很多文件夹(az& AZ),所以我只是提出多个请求,使其列出所有文件,
This code is to list mp4, pdf, png, jpg or all files
//letter range a-z and A-Z
$az = range('a', 'z');
$AZ = range('A', 'Z');
//To get the total no of files
$total = 0;
//text file
$File = "CDNFileList.txt";
//getting dropdownlist values
$selectedoption = $_POST['cdn_dropdown_list'];
$file_ext = '';
if ($selectedoption == 'pdf'){
$file_ext = 'PDF DOCUMENTS';
}else if(($selectedoption == 'jpg')){
$file_ext = 'JPEG IMAGES';
}else if(($selectedoption == 'png')){
$file_ext = 'PNG IMAGES';
}else if($selectedoption == 'mp4'){
$file_ext = 'MP4 VIDEOS';
}else if($selectedoption == 'all'){
$file_ext = 'ALL CONTENTS';
}
//Creating table
echo "<table style='width:300px' border='1'><th colspan='2'><b>List of $file_ext</b></th><tr><td><b>Name of the File</b></td><td><b>URL of the file</b></td></tr>";
foreach($az as $value){
$response = $s3Client->listObjects(array('Bucket' => $bucket, 'MaxKeys' => 1000, 'Prefix' => 'files/'.$value));
$files = $response->getPath('Contents');
$file_list = array();
foreach ($files as $file) {
$filename = $file['Key'];
if ( 'all' == ($selectedoption)){
$file_path_parts = pathinfo($filename);
$file_name = $file_path_parts['filename'];
echo "<tr><td>$file_name</td><td><a href = '";
echo $baseUrl.$filename;
echo "' target='_blank'>";
echo $baseUrl.$filename;
echo "</a></td></tr>";
$filename = $baseUrl.$filename.PHP_EOL;
array_push($file_list, $filename);
$total++;
}else{
$filetype = strtolower(substr($filename, strrpos($filename, '.')+1));
if ($filetype == ($selectedoption)){
$file_path_parts = pathinfo($filename);
$file_name = $file_path_parts['filename'];
echo "<tr><td>$file_name</td><td><a href = '";
echo $baseUrl.$filename;
echo "' target='_blank'>";
echo $baseUrl.$filename;
echo "</a></td></tr>";
$filename = $baseUrl.$filename.PHP_EOL;
array_push($file_list, $filename);
$total++;
}
}
}
}
foreach($AZ as $value){
$response = $s3Client->listObjects(array('Bucket' => $bucket, 'MaxKeys' => 1000, 'Prefix' => 'files/'.$value));
$files = $response->getPath('Contents');
$file_list = array();
foreach ($files as $file) {
$filename = $file['Key'];
if ( 'all' == ($selectedoption)){
$file_path_parts = pathinfo($filename);
$file_name = $file_path_parts['filename'];
echo "<tr><td>$file_name</td><td><a href = '";
echo $baseUrl.$filename;
echo "' target='_blank'>";
echo $baseUrl.$filename;
echo "</a></td></tr>";
$filename = $baseUrl.$filename.PHP_EOL;
array_push($file_list, $filename);
$total++;
}else{
$filetype = strtolower(substr($filename, strrpos($filename, '.')+1));
if ($filetype == ($selectedoption)){
$file_path_parts = pathinfo($filename);
$file_name = $file_path_parts['filename'];
echo "<tr><td>$file_name</td><td><a href = '";
echo $baseUrl.$filename;
echo "' target='_blank'>";
echo $baseUrl.$filename;
echo "</a></td></tr>";
$filename = $baseUrl.$filename.PHP_EOL;
array_push($file_list, $filename);
$total++;
}
}
}
}
echo "</table><br/>";
print "\n\nTOTAL NO OF $file_ext ".$total;
这只是此问题的解决方法,因为没有AWS API
列出所有文件(超过1000个)。希望它可以帮助某人。
答案 2 :(得分:0)
使用Paginator获取所有文件
$client = new S3Client([
'version' => AWS_S3_CLIENT_FACTORY_VERSION,
'region' => AWS_S3_CLIENT_FACTORY_REGION,
]);
$objects = $client->getPaginator('ListObjects', ['Bucket' => "my-bucket"]);
foreach ($objects as $listResponse) {
$items = $listResponse->search("Contents[?starts_with(Key,'path/to/folder/')]");
foreach($items as $item) {
echo $item['Key'] . PHP_EOL;
}
}
要获取所有文件,请将搜索更改为:
$listResponse->search("Contents[*]");