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

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

Automatic Streaming

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');

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

Hope it helps


You can try this code

$contents = Storage::get($file);
Storage::disk('s3')->put($newfile,$contents);

As Laravel document this is the easy way I found to copy data between two disks


There is a way to copy files without needing to load the file contents into memory using MountManager.

You will also need to import the following:

use League\Flysystem\MountManager;

Now you can copy the file like so:

$mountManager = new MountManager([
    's3' => \Storage::disk('s3')->getDriver(),
    'local' => \Storage::disk('local')->getDriver(),
]);
$mountManager->copy('s3://path/to/file.txt', 'local://path/to/output/file.txt');

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);