dongyi8383 2017-01-30 12:07
浏览 330
已采纳

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

I'm having some troubles listing all the files within certain prefix of an AWS S3 Bucket.

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'] . "
";
    }
} catch (S3Exception $e) {
    echo $e->getMessage() . "
";
}

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.

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.

I have tried removing the final / on the prefix, as well as using 'listObjects':

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

and even with trying a different method of just ListObjects or ListObjectsV2 (found this information). 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!!!

Any help would be much appreciated.

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

  • 写回答

1条回答 默认 最新

  • douchen3562 2017-01-30 18:49
    关注

    You are missing most important key to set which is

    like this // 'ap-southeast-1' for asian region

                'region'  => 'ap-southeast-1',
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 灰狼算法和蚁群算法如何结合
  • ¥15 这是一个利用ESP32自带按键和LED控制的录像代码,编译过程出现问题,请解决并且指出错误,指导如何处理 ,协助完成代码并上传代码
  • ¥20 stm32f103,hal库 hal_usart_receive函数接收不到数据。
  • ¥20 求结果和代码,sas利用OPTEX程序和D-efficiency生成正交集
  • ¥50 求fpga交通信号灯设计Verilog代码
  • ¥50 adb连接不到手机是怎么回事?
  • ¥20 抓取数据时发生错误: get_mooncake_data() missing 1 required positional argument: 'driver'的问题,怎么改出正确的爬虫代码?
  • ¥15 vs2022无法联网
  • ¥15 TCP的客户端和服务器的互联
  • ¥15 VB.NET操作免驱摄像头