我正在尝试从亚马逊S3存储桶子文件夹中获取所有文件,并使其可以在网页中下载。
我有一个叫做图像的桶。在那个桶里面我有一些其他的文件夹。现在我试图获取该子文件夹中的所有文件并在页面中显示它。 S3铲斗: /图片 /图像/ TEST1 / /图像/ TEST2 / /图像/ TEST1 / 1 /图像/ TEST1 / 2 /图像/ TEST1 / 1 /项目
我试过这样,但无法得到预期的结果。
// 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
哪个需要是可下载的链接。
请建议我链接或给我一些想法我该怎么做。
答案 0 :(得分: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;
}