Get files from amazon s3 buckets sub folder Get files from amazon s3 buckets sub folder codeigniter codeigniter

Get files from amazon s3 buckets sub folder


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;}