AWS S3 PHP SDK V3如何直接转到存储桶

时间:2016-06-12 03:39:24

标签: php amazon-web-services amazon-s3

我一直在尝试学习这个SDK并且我开始理解它,但我似乎无法弄清楚的一个问题是如何直接进入“文件夹”(我知道没有文件夹)在桶中? 在我的存储桶中,我有“folder1”,使用getIterator('ListObjects',...我得到的只是显示“folder1”的存储桶的根,而不是“folder1”的根。 我使用了'Prefix'和'Delimiter',但我想我错过了一些东西。

我得到了,感谢任何帮助。

$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):...

我在这里缺少什么?

<?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>

0 个答案:

没有答案