Laravel 5:如何将本地文件复制到Amazon S3? [英] Laravel 5: How do you copy a local file to Amazon S3?

查看:227
本文介绍了Laravel 5:如何将本地文件复制到Amazon S3?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Laravel 5中编写代码以定期备份MySQL数据库.到目前为止,我的代码如下:

I'm writing code in Laravel 5 to periodically backup a MySQL database. My code thus far looks like this:

    $filename = 'database_backup_'.date('G_a_m_d_y').'.sql';
    $destination = storage_path() . '/backups/';

    $database = \Config::get('database.connections.mysql.database');
    $username = \Config::get('database.connections.mysql.username');
    $password = \Config::get('database.connections.mysql.password');

    $sql = "mysqldump $database --password=$password --user=$username --single-transaction >$destination" . $filename;

    $result = exec($sql, $output); // TODO: check $result

    // Copy database dump to S3

    $disk = \Storage::disk('s3');

    // ????????????????????????????????
    //  What goes here?
    // ????????????????????????????????

我在网上看到了一些解决方案,建议我做些类似的事情:

I've seen solutions online that would suggest I do something like:

$disk->put('my/bucket/' . $filename, file_get_contents($destination . $filename));

但是,对于大文件,使用file_get_contents()是否浪费呢?有更好的解决方案吗?

However, for large files, isn't it wasteful to use file_get_contents()? Are there any better solutions?

推荐答案

Laravel现在具有putFileputFileAs方法来允许文件流.

Laravel has now putFile and putFileAs method to allow stream of file.

自动流式传输

如果您希望Laravel自动管理 将给定文件流式传输到您的存储位置,则可以使用 putFile或putFileAs方法.此方法接受一个 Illuminate \ Http \ File或Illuminate \ Http \ UploadedFile实例,并将 自动将文件流式传输到您想要的位置:

If you would like Laravel to automatically manage streaming a given file to your storage location, you may use the putFile or putFileAs method. This method accepts either a Illuminate\Http\File or Illuminate\Http\UploadedFile instance and will automatically stream the file to your desired location:

use Illuminate\Http\File;
use Illuminate\Support\Facades\Storage;

// Automatically generate a unique ID for file name...
Storage::putFile('photos', new File('/path/to/photo'));

// Manually specify a file name...
Storage::putFileAs('photos', new File('/path/to/photo'), 'photo.jpg');

链接到文档: https://laravel.com/docs/5.8/filesystem (自动流式传输)

Link to doc: https://laravel.com/docs/5.8/filesystem (Automatic Streaming)

希望有帮助

这篇关于Laravel 5:如何将本地文件复制到Amazon S3?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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