AWS s3在特定文件夹中下载所有文件-使用PHP SDK [英] AWS s3 download all file inside a specific folder - Using PHP SDK

查看:1073
本文介绍了AWS s3在特定文件夹中下载所有文件-使用PHP SDK的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮我这个吗.. ??

Can anyone help me for this one..??

我想将所有文件从存储桶文件夹内的文件夹下载到同名计算机目录中.

I want to download all files from a folder which is inside my bucket's folder, to my computer's directory with the same name.

假设桶名称为"ABC",其中有一个文件夹为"DEF"..在该文件夹中,有多个文件可用..

Let's say there is a bucket name "ABC" a folder is there inside it, is "DEF".. In which folder there are multiple files available..

现在我想将其下载到我的项目文件夹"/opt/lampp/htdocs/porject/files/download/"中,这里也有"DEF"文件夹.

Now I want to download it into my project folder "/opt/lampp/htdocs/porject/files/download/" here "DEF" folder is also available..

那么,任何人都可以帮助我,并为我提供代码.?

So, anyone can help me, and give me the code for this..?

先谢谢了.

=============

=============

错误:

致命错误:消息为'RecursiveDirectoryIterator :: __ em> construct()[recursivedirectoryiterator .-- construct]的未捕获异常'UnexpectedValueException':无法找到包装"s3"; -您在配置PHP时是否忘记启用它?"在/opt/lampp/htdocs/demo/amazon-s3/test.php:21中的堆栈跟踪:#0/opt/lampp/htdocs/demo/amazon-s3/test.php(21):RecursiveDirectoryIterator-> _construct('s3://bucketname/folder ...')#1 {main}在第21行的/opt/lampp/htdocs/demo/amazon-s3/test.php中抛出

Fatal error: Uncaught exception 'UnexpectedValueException' with message 'RecursiveDirectoryIterator::_construct() [recursivedirectoryiterator.--construct]: Unable to find the wrapper "s3" - did you forget to enable it when you configured PHP?' in /opt/lampp/htdocs/demo/amazon-s3/test.php:21 Stack trace: #0 /opt/lampp/htdocs/demo/amazon-s3/test.php(21): RecursiveDirectoryIterator->_construct('s3://bucketname/folder...') #1 {main} thrown in /opt/lampp/htdocs/demo/amazon-s3/test.php on line 21

推荐答案

使用Amazon S3流包装器非常简单:

Pretty straightforward using the Amazon S3 stream wrapper:

include dirname(__FILE__) . '/aws.phar';
$baseDirectory = dirname(__FILE__) .'/'.$myDirectoryName;


$client = \Aws\S3\S3Client::factory(array(
    'key'    => "<my key>",
    'secret' => "<my secret>"
));

$client->registerStreamWrapper();


$bucket = 's3://mys3bucket/' . $myDirectoryName

$iterator = new RecursiveIteratorIterator(
    new RecursiveDirectoryIterator($bucket),
    RecursiveIteratorIterator::SELF_FIRST
);

foreach($iterator as $name => $object) {
    if ($object->getFileName() !== '.' && $object->getFileName() !== '..') {
        $relative = substr($name,strlen($bucket)+1);
        if (!file_exists($baseDirectory . '/' . $path . '/' . $relative)) {
            if ($object->isDir()) {
                mkdir($baseDirectory . '/' . $path . '/' . $relative, 0777, true);
            } else {
                file_put_contents(
                    $baseDirectory . '/' . $path . '/' . $relative,
                    file_get_contents($name)
                );
            }
        }
    }
}

这篇关于AWS s3在特定文件夹中下载所有文件-使用PHP SDK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆