[英]iterating through S3 bucket with stream wrapper
我正在尝试列出Amazon S3存储桶中的所有项目。 我有几个嵌套的目录。
每个子目录包含几个文件。 我需要使用此文件结构获取嵌套数组。
我正在使用适用于PHP 2.4.2的Amazon AWS开发工具包
这是我的代码:
$dir = 's3://bucketname';
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir));
foreach ($iterator as $file) {
echo $file->getType() . ': ' . $file . "\n";
}
但是,结果仅列出存储桶中的文件,而不列出目录/子目录中的文件(带前缀的文件)或目录本身。
如果我遍历($dir.'/folder')
根本没有结果。
我将RecursiveIteratorIterator::SELF_FIRST
作为迭代器的构造函数的第二个参数传递,我仅获得第一级目录,而没有子目录。
如何使用AWS流包装器和PHP RecursiveIterator列出存储桶中所有目录中的所有文件?
我希望有一个人可以帮助我。
谢谢!
我有同样的问题,我使用以下方法解决了这个问题:
use \Aws\S3\StreamWrapper;
use \Aws\S3\S3Client;
private $files = array();
private $s3path = 'YOUR_BUCKET';
private $s3key = 'YOUR_KEY';
private $s3auth = 'YOUR_AUTH_CODE';
public function recursive($path)
{
$dirHandle = scandir($path);
foreach($dirHandle as $file)
{
if(is_dir($path.$file."/") && $file != '.' && $file != '..')
{
$this->recursive($path.$file."/");
}
else
{
$this->files[$path.$file] = $path.$file;
}
}
}
public function registerS3()
{
$client = S3Client::factory(array(
'key' => $this->s3key,
'secret' => $this->s3auth
));
$wp = new StreamWrapper();
$wp->register($client);
}
public function run()
{
$folder = 's3://'.$this->s3path.'/';
$this->registerS3();
$this->recursive($folder);
}
现在,如果您在$ this-> files中执行DUMP,则应显示存储桶中的所有文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.