简体   繁体   English

使用 AWS PHP SDK 列出 AWS S3 存储桶内文件夹(前缀)中的所有文件

[英]List all files within a folder (prefix) inside an AWS S3 Bucket using the AWS PHP SDK

I'm having some troubles listing all the files within certain prefix of an AWS S3 Bucket.我在列出 AWS S3 存储桶的某个前缀内的所有文件时遇到了一些麻烦。

I have searched and search, but only seem to be able to find the same information:我搜索和搜索,但似乎只能找到相同的信息:

try {
$objects = $s3Client->getIterator('ListObjects', array(
    'Bucket' => 'BUCKETNAME',
    'Prefix' => '/uploads/content/document/'
));
    foreach ($objects as $object) {
        echo $object['Key'] . "\n";
    }
} catch (S3Exception $e) {
    echo $e->getMessage() . "\n";
}

This will give back a fair bit of information if I print_r($objects) however, nothing is echo'd out in the loop, and no matter what I try, I can't seem to get it to list any files.如果我print_r($objects) ,这将返回相当多的信息,但是,循环中没有任何内容被回显,无论我尝试什么,我似乎都无法让它列出任何文件。

I actually have another 6 folders I want to retrieve a list of files from, however, obviously need to get it working with just document for now.我实际上还有另外 6 个文件夹,我想从中检索文件列表,但是,显然现在需要让它只处理document

I have tried removing the final / on the prefix, as well as using ' listObjects ':我尝试删除前缀上的最后一个 / ,以及使用“ listObjects ”:

$s3Client->getIterator('listObjects' ...

and even with trying a different method of just ListObjects or ListObjectsV2 (found this information).甚至尝试使用ListObjectsListObjectsV2的不同方法(找到此信息)。 The first one works but again doesn't list any files, and the second method isn't found.第一个有效,但同样没有列出任何文件,第二个方法也没有找到。

$s3Client->ListObjects(array( ...
$s3Client->ListObjectsV2(array( ...

No doubt i'm missing something.毫无疑问,我错过了一些东西。 All I want to achieve is the list of all files names within each of the folders/prefixes, so I can list these and use some clever jQuery to add them into a document when selected in our CMS, sure there should be more information on this somewhere!!!我想要实现的只是每个文件夹/前缀中所有文件名的列表,所以我可以列出这些并使用一些聪明的 jQuery 在我们的 CMS 中选择时将它们添加到文档中,确保应该有更多关于此的信息某处!!!

Any help would be much appreciated.任何帮助将非常感激。

print_r($objects) returns: http://pastebin.com/YwWLLsSK print_r($objects)返回: http : //pastebin.com/YwWLLsSK

You are missing most important key to set which is您缺少最重要的键来设置哪个是

like this // 'ap-southeast-1' for asian region像这样//亚洲地区的'ap-southeast-1'

            'region'  => 'ap-southeast-1',

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM