Recursively Transfer Entire Directory to Amazon S3 with Laravel 5.2
Recursively Transfer Entire Directory to Amazon S3 with Laravel 5.2
Jason Jason Photo By Jason Jason, Apr 25, 2016

I've been working with a lot of large video files recently and I needed to transfer an entire directory with all it's files, folders, nested subdirectories, etc. up to Amazon S3 for storage. Normally, transferring a file up to Amazon S3 is really easy with Laravel 5.2 using the built-in Filesystem. The problem that I found was that this method works very well when transferring one file at a time, but it doesn't allow you to recursively transfer all the contents of a directory. So I did some research and came up with this code that can easily transfer up all the contents of a directory to Amazon S3.


  • Laravel 5.2 installed and ready to go
  • Amazon S3 account with a unique "bucket" to upload your files to

Install AWS Package

Add the following to your composer.json file under the require section:

"league/flysystem-aws-s3-v3": "~1.0"

and then run composer update via the terminal from the laravel root directory.

Add Amazon Keys and Settings

Add the following to the end of your .env file:

AMAZON_KEY="[your Amazon key which may be obtained your Amazon account]"
AMAZON_SECRET="[your Amazon secret which may be obtained your Amazon account]"
AMAZON_REGION="[your region. I tend to use 'us-west-1']"
AMAZON_BUCKET="[your bucket name]"

Uploading a Directory

Once you've installed the AWS package and added the Amazon keys and settings to your .env file, you can add the following to your controller/command method:

// Upload to Amazon S3
$client = S3Client::factory(array(
    'credentials' => [
        'key'    => env('AMAZON_KEY'),
        'secret' => env('AMAZON_SECRET'),
    'region'  => env('AMAZON_REGION'),
    'version' => env('AMAZON_VERSION'),
$path = storage_path() . '/upload_to_s3'; // change "upload_to_s3" to the directory you'd like to upload
$dest = 's3://'.env('AMAZON_BUCKET').'/uploads'; // change "uploads" to where ever you'd like your directory to be

// Create a transfer object.
$manager = new Transfer($client, $path, $dest);

// Perform the transfer synchronously.


Tags & Categories