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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22165094/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:42:36  来源:igfitidea点击:

Quick way to list all files in Amazon S3 bucket using php?

phpamazon-web-servicesamazon-s3cdn

提问by Hitesh

I have an amazon s3 bucket that has tens of thousands of filenames in it. What's the easiest way to get a list of all file or text file that lists all the filenames in the bucket?

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

I have tried with listObject(), but It seems that it only list 1000 files.

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

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

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 PHPbut in aws docs I read

-->使用适用于 PHP 的 AWS 开发工具包列出密钥,但在我阅读的 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.

max-keys - 字符串 - 可选 - 方法调用返回的最大结果数。返回的列表不会包含比指定值更多的结果,但可能返回更少。默认值为 1000。

AWS DOC FOR list_objects

AWS DOC FOR list_objects

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

有没有办法列出所有内容并使用 AWS PHP SDK 将其打印到文本文件?

Possible repeat :quick-way-to-list-all-files-in-amazon-s3-bucket

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

I have reposted the question because am looking for the solution in php.

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

Code :

代码 :

$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;

 }

回答by Jeremy Lindblom

To get more than 1000 objects, you must make multiple requests using the Markerparameter to tell S3 where you left off for each request. Using the Iteratorsfeature of the AWS SDK for PHP makes it easier to get all of your objects, because it encapsulates the logic of making multiple API requests. Try this:

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

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

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

With latest PHP SDK (as of March 2016) the code must be written like this instead:

使用最新的 PHP SDK(截至 2016 年 3 月),代码必须这样编写:

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

回答by Hitesh

Below code is just one trick, work around for this problem, I have pointed to my CDN bucketfolder which have lot of folder alphabetically (a-z & A-Z), so I just made a multiple requests to make it list all files,

下面的代码只是一个技巧,解决这个问题,我已经指向我的CDN bucket文件夹,其中有很多按字母顺序排列的文件夹(az 和 AZ),所以我只是提出了多个请求,让它列出所有文件,

This code is to list mp4, pdf, png, jpg or all files

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;

This is just a workaround for this problem,Since there is no AWS APIto list all the files (more than 1000). hope it helps someone.

这只是解决此问题的方法,因为没有AWS API列出所有文件(超过 1000 个)。希望它可以帮助某人。

回答by user563093

Use Paginator to get all files

使用分页器获取所有文件

    $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;
        }
    }

To get all files change the search to:

要获取所有文件,请将搜索更改为:

$listResponse->search("Contents[*]");