I have been trying to learn this SDK and im starting to understand it, but one issue I cant seem to figure out is how do I go directly to a "folder" (I know there are not folders) in the bucket? In my bucket I have "folder1", using getIterator('ListObjects',... all I get is the root of the bucket showing "folder1" instead of root of "folder1". I have used the 'Prefix' and 'Delimiter' but i guess I am missing something.
Here's I got, any help is appreciated.
$s3 = new S3Client([
'version' => 'latest',
'region' => 'us-east-1',
'credentials' => [
'key' => $config['s3']['key'],
'secret' => $config['s3']['secret'],
],]);
$objects = $s3->getIterator('ListObjects', array(
'Bucket' => $config['s3']['bucket'],
'Prefix' => $config['s3']['folder1']."/",
'Delimiter' => "/",
));
foreach($objects as $object):...
What am I missing here?
<?php
$i = 0; $y = 1;
$imgExts = array('jpg', 'jpeg', 'tiff', 'tif', 'gif', 'png', 'bmp', 'svg');
$videoExts = array('mp4', 'avi', 'mpg', 'mpeg', '3gp', 'mov', 'mkv');
$folderArr = array(); // array of the folders get from the List object
//foreach($objects as $object):
//foreach($objects['Contents'] as $object):
foreach ($objects as $result) {
// You can extract any data that you want from the result.
foreach ($result['Contents'] as $object):
$size = $size+$object['Size'];
$filepath = $object['Key'];
echo $filepath;
$tmp = explode("/", $filepath); // object key is exploded by "/" to get check whether it is a file or a directory.
//echo $tmp;
$filename = array_pop($tmp); // return last element from array, it will be file name or blank value
$directoryPath = implode("/", $tmp)."/"; // implode array using "/" to get directory of the object
$SubFolderPath = str_replace(".php","",$_SERVER['PHP_SELF']);
$tmpFol = explode("/",trim($directoryPath, "/"));
$subDir = array_pop($tmpFol);
$rootDir = implode("/", $tmpFol)."/";
//echo $tmpFol;
if(!empty($filename)):
$img = "";
$ext = array_pop(explode(".", $filename));
if(in_array(strtolower(trim($ext)), $imgExts)):
$img = "fa fa-camera-retro fa-lg";
//$img = $s3->getObjectUrl($config['s3']['bucket'], $filepath);
elseif(in_array(strtolower(trim($ext)), $videoExts)):
$img = "fa fa-video-camera";
else:
$img = "fa fa-file-text-o";
endif;
endif;
$fileContent = '<tr class="file-sort" data-name="'.strtolower($filename).'" data-date="'.date("Y-m-d", strtotime($object['LastModified'])).'>" data-size="'.$object['Size'].'">
<td>'.$y.'</td>
<td><i class="'.$img.'"></i> '.$filename.'</td>
<!--<td><a href="'.$img.'" data-lightbox="image-set" data-title="'.strtolower($filename).'" ><img src="'.$img.'"style="width:65px;height:60px;cursor:zoom-in;" class="center_image"><span style="cursor:zoom-in">'.$filename.'</span></td>-->
<td>'.date("M j Y, g:i A", strtotime($object['LastModified'])).'</td>
<td>'.formatBytes($object['Size'], 0).'</td>
<td>
<a href="download_file.php?key='.$s3->getObjectUrl($config['s3']['bucket'], $filepath).'" download="'.$filename.'" class="btn btn-primary"><i class="fa fa-cloud-download"></i></a>