簡體   English   中英

使用流包裝器在S3存儲桶中進行迭代

[英]iterating through S3 bucket with stream wrapper

我正在嘗試列出Amazon S3存儲桶中的所有項目。 我有幾個嵌套的目錄。

  • DIR1 /
  • DIR1 / subdir1 /
  • DIR1 / subdir2 /
  • DIR1 / subdir3 /
  • DIR2 /
  • DIR2 / subdir1 /
  • DIR2 / subdir2 /
  • ...

每個子目錄包含幾個文件。 我需要使用此文件結構獲取嵌套數組。

我正在使用適用於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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM