我正在 Laravel 5 中编写代码来定期备份 MySQL 数据库。到目前为止,我的代码如下所示:
$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?
// ????????????????????????????????
我在网上看到过一些建议我做以下事情的解决方案:
$disk->put('my/bucket/' . $filename, file_get_contents($destination . $filename));
但是,对于大文件,使用file_get_contents()是不是很浪费?有没有更好的解决方案?
最佳答案
Laravel 现在有 putFile
和 putFileAs
方法来允许文件流。
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(自动流媒体)
希望能帮助到你
关于Laravel 5 : How do you copy a local file to Amazon S3?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29527611/