问题描述
我想从一个Amazon S3的桶子文件夹中的所有文件,并让他们在一个网页下载。
我有一个名为图像桶。在那个桶我有一些其他的文件夹。现在我试图让所有的子文件夹里面的文件,并显示在页面中。 S3铲斗: /照片 /图像/测试1 / /图像/测试2 / /图像/测试1/1 /图像/测试1/2 /图像/测试1/1 /条
我已经试过这样的,但无法得到预期的结果。
//目标文件的完整路径:图像/测试1/1 /个
$ bucketName ='图像';
$源='/测试1/1 /项'
$图像= $这个 - > S3-> getBucket($ bucketName);
的foreach($形象为$关键=> $数据){
$ K = $数据['名称'];
的print_r($ K); //现在这给了我一切的图像桶内的完整列表。
}
现在,如果我想从桶里的对象,我可以使用getObject,我试过这样的:
$结果= $这个 - > S3->的getObject($图像); //我很困惑这个
任何建议将AP preciated。谢谢你,
使用下面的回答若苏埃伊瓦拉后,我得到了这样的事情
(
[tableuploads / 1 / emaillists / 15.10.2013-18.03.23-emailList.csv] =>排列
(
[文件名] => 15.10.2013-18.03.23-emailList.csv
[file_folder] => emaillists
[FILE_SIZE] => 64
[created_on] => 2013年10月15日十九点03分26秒
[s3_link] => http://s3.amazonaws.com/webtools_sharing/emaillists/15.10.2013- 23年3月18日,emailList.csv
[MD5_HASH] => 4809ae0b75d3517b69f69b53ba0b2959
)
[tableuploads / 1 / emaillists / 15.10.2013-18.04.32-emailList.csv] =>排列
(
[文件名] => 15.10.2013-18.04.32-emailList.csv
[file_folder] => emaillists
[FILE_SIZE] => 64
[created_on] => 2013年10月15日十九时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] =>排列
(
[文件名] => 15.10.2013-18.03.23-emailList.csv
[file_folder] => emaillists
[FILE_SIZE] => 64
[created_on] => 2013年10月15日十九点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] =>排列
(
[文件名] => 15.10.2013-18.04.32-emailList.csv
[file_folder] => emaillists
[FILE_SIZE] => 64
[created_on] => 2013年10月15日十九时04分45秒
[s3_link] => http://s3.amazonaws.com/webtools_sharing/emaillists/15.10.2013-18.04.32-emailList.csv
[MD5_HASH] => 14094e133779619ddfcfc008d16ce75b
)
)
我想是这样的: 的 emailList.csv
<$p$p><$c$c>http://s3.amazonaws.com/webtools_sharing/tableuploads/2/emaillists/15.10.2013-18.03.23-emailList.csv其中需要是可下载的链接。
请建议我一个链接或者给我一些想法,我怎么能做到这一点。
我做了这个功能,而回
公共职能list_s3_bucket($ bucket_name)
{
//初始化数据数组
$的数据;
$ bucket_content = $这个 - &GT; S3-&GT; getBucket($ bucket_name);
的foreach($ bucket_content为$关键=&GT; $值){
//忽略S3文件夹
如果(preg_match(/ \ / $ /,$键))继续;
//爆炸的路径到一个数组
$ FILE_PATH =爆炸('/',$键);
$ FILE_NAME =结束($ FILE_PATH);
$ file_folder = SUBSTR($关键,0,(的strlen($ FILE_NAME)* -1)+1);
$ file_folder = preV($ FILE_PATH);
$ s3_url =https://s3.amazonaws.com/{$bucket_name}/{$key};
$数据[$关键] =阵列(
FILE_NAME'=&GT; $ FILE_NAME,
s3_key'=&GT; $关键,
file_folder'=&GT; $ file_folder,
FILE_SIZE'=&GT; $值['大小'],
created_on'=&GT;日期(Y-M-D H:我:S',$值['时间']),
s3_link'=&GT; $ s3_url,
MD5_HASH'=&GT; $值['散']);
}
返回$的数据;
}
这将返回一个数组,关键是完整的文件名,所以你可以做:
$名单= $这个 - &GT; your_model-&GT; list_s3_bucket($ bucket_name);
的foreach($清单,$关键=&GT; $行){
force_download($这个 - &GT; S3-&GT;的getObject($行['s3_key']),$行['FILE_NAME']);
//你可以使用这个网址:
打印($行['s3_link']);
}
功能force_download($数据,$ FILE_NAME)
{
标题(内容类型:应用程序/八位字节流);
标题(内容处置:附件;文件名= \{$ FILE_NAME} \);
回声$的数据;
}
I am trying to get all the files from a amazon S3 buckets sub folder and make them downloadable in a web page.
I have a bucket called images. Inside that bucket I have some other folders. Now I am trying to get all the files inside that subfolder and show it in a page. S3 Buckets: /images /images/test1/ /images/test2/ /images/test1/1 /images/test1/2 /images/test1/1/item
I have tried like this, but couldn't get the expected result.
// 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.
}
Now if I want to get objects from the bucket, I can use getObject, which I tried like this:
$result = $this->s3->getObject($image); // I am confused about this one
Any suggestions will be appreciated. Thanks,
After using the following answer from Josue Ibarra, I got something like this
(
[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
)
)
I want something like this: 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
Which needs to be a downloadable link.
Please suggest me to a link or give me some ideas how can I do it.
I made this function a while back
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;
}
It returns you an array, the key is the full file name, so you can do:
$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;
}
这篇关于从Amazon S3的桶子文件夹中获取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!