Laravel 5: How do you copy a local file to Amazon S3?

后端 未结 6 2105
心在旅途
心在旅途 2021-02-08 11:53

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\         


        
6条回答
  •  情书的邮戳
    2021-02-08 12:32

    You can always use a file resource to stream the file (advisable for large files) by doing something like this:

    Storage::disk('s3')->put('my/bucket/' . $filename, fopen('path/to/local/file', 'r+'));
    

    An alternative suggestion is proposed here. It uses Laravel's Storage facade to read the stream. The basic idea is something like this:

        $inputStream = Storage::disk('local')->getDriver()->readStream('/path/to/file');
        $destination = Storage::disk('s3')->getDriver()->getAdapter()->getPathPrefix().'/my/bucket/';
        Storage::disk('s3')->getDriver()->putStream($destination, $inputStream);
    

提交回复
热议问题