5

我正在尝试从亚马逊 S3 存储桶子文件夹中获取所有文件,并使其可在网页中下载。

我有一个名为图像的存储桶。在那个桶里面我有一些其他的文件夹。现在我正在尝试获取该子文件夹中的所有文件并将其显示在页面中。S3 存储桶: /images /images/test1/ /images/test2/ /images/test1/1 /images/test1/2 /images/test1/1/item

我试过这样,但无法得到预期的结果。

// Target files full path : images/test1/1/item 

$bucketName = 'images';
$source = '/test1/1/item' 

$image = $this->s3->getBucket($bucketName);
foreach ($image as $key=>$data){
   $k = $data['name'];
   print_r($k); // now this gives me full list of everything inside the images bucket. 
}

现在,如果我想从存储桶中获取对象,我可以使用 getObject,我试过这样:

$result = $this->s3->getObject($image); // I am confused about this one

任何建议将不胜感激。谢谢,

在使用 Josue Ibarra 的以下答案后,我得到了这样的结果

(

 [tableuploads/1/emaillists/15.10.2013-18.03.23-emailList.csv] => Array
     (
         [file_name] => 15.10.2013-18.03.23-emailList.csv
         [file_folder] => emaillists
         [file_size] => 64
         [created_on] => 2013-10-15 19:03:26
         [s3_link] => http://s3.amazonaws.com/webtools_sharing/emaillists/15.10.2013-       18.03.23-emailList.csv
        [md5_hash] => 4809ae0b75d3517b69f69b53ba0b2959
    )

[tableuploads/1/emaillists/15.10.2013-18.04.32-emailList.csv] => Array
    (
        [file_name] => 15.10.2013-18.04.32-emailList.csv
        [file_folder] => emaillists
        [file_size] => 64
        [created_on] => 2013-10-15 19:04:45
        [s3_link] => http://s3.amazonaws.com/webtools_sharing/emaillists/15.10.2013-18.04.32-emailList.csv
        [md5_hash] => 14094e133779619ddfcfc008d16ce75b
    )

[tableuploads/2/emaillists/15.10.2013-18.03.23-emailList.csv] => Array
    (
        [file_name] => 15.10.2013-18.03.23-emailList.csv
        [file_folder] => emaillists
        [file_size] => 64
        [created_on] => 2013-10-15 19:03:26
        [s3_link] => http://s3.amazonaws.com/webtools_sharing/emaillists/15.10.2013-18.03.23-emailList.csv
        [md5_hash] => 4809ae0b75d3517b69f69b53ba0b2959
    )

[tableuploads/2/emaillists/15.10.2013-18.04.32-emailList.csv] => Array
    (
        [file_name] => 15.10.2013-18.04.32-emailList.csv
        [file_folder] => emaillists
        [file_size] => 64
        [created_on] => 2013-10-15 19:04:45
        [s3_link] => http://s3.amazonaws.com/webtools_sharing/emaillists/15.10.2013-18.04.32-emailList.csv
        [md5_hash] => 14094e133779619ddfcfc008d16ce75b
    )
)

我想要这样的东西: http : //s3.amazonaws.com/webtools_sharing/tableuploads/1/emaillists/15.10.2013-18.03.23-emailList.csv

http://s3.amazonaws.com/webtools_sharing/tableuploads/2/emaillists/15.10.2013-18.03.23-emailList.csv

这需要是一个可下载的链接。

请建议我链接或给我一些想法,我该怎么做。

4

1 回答 1

3

我不久前做了这个功能

public function list_s3_bucket($bucket_name)
{
    // initialize the data array
    $data;
    $bucket_content = $this->s3->getBucket($bucket_name);

    foreach ($bucket_content as $key => $value) {
        // ignore s3 "folders"
        if (preg_match("/\/$/", $key)) continue;

        // explode the path into an array
        $file_path = explode('/', $key);
        $file_name = end($file_path);
            $file_folder = substr($key, 0, (strlen($file_name) * -1)+1);
        $file_folder = prev($file_path);

        $s3_url = "https://s3.amazonaws.com/{$bucket_name}/{$key}";

        $data[$key] = array(
            'file_name' => $file_name,
                    's3_key' => $key,
            'file_folder' => $file_folder,
            'file_size' => $value['size'],
            'created_on' => date('Y-m-d H:i:s', $value['time']),
            's3_link' => $s3_url,
            'md5_hash' => $value['hash']);
    }
    return $data;
}

它返回一个数组,键是完整的文件名,所以你可以这样做:

$list = $this->your_model->list_s3_bucket($bucket_name);

foreach ($list as $key => $row){
   force_download($this->s3->getObject($row['s3_key']), $row['file_name']);
   // you can use this url:
   print($row['s3_link']);
}

function force_download($data, $file_name)
{
      header("Content-type: application/octet-stream");
      header("Content-Disposition: attachment; filename=\"{$file_name}\"");
      echo $data;
}
于 2013-10-17T19:27:49.580 回答