Skip to content

Instantly share code, notes, and snippets.

@FraGoTe
Forked from vaibhavpandeyvpz/pu2s3.php
Created June 6, 2022 15:33
Show Gist options
  • Save FraGoTe/279682e03893633a2d9ca6bf8fd978bd to your computer and use it in GitHub Desktop.
Save FraGoTe/279682e03893633a2d9ca6bf8fd978bd to your computer and use it in GitHub Desktop.
Upload files from public (or any) disk to AWS S3 in Laravel without using any external tools.
<?php
use Illuminate\Support\Facades\Storage;
$public = Storage::disk('public');
$s3 = Storage::disk('s3');
$files = $public->files(null, true);
foreach ($files as $file) {
echo 'Uploading ', $file, '...', PHP_EOL;
$s3->put($file, $public->readStream($file), ['visibility' => 'public']);
}

To use it, first set the below variables in your .env and run php artisan config:cache.

AWS_BUCKET=???
AWS_ACCESS_KEY_ID=???
AWS_SECRET_ACCESS_KEY=???
AWS_DEFAULT_REGION=???
AWS_ENDPOINT=???

In project folder, create a file named e.g., pu2s3.php and paste the Gist's content in it. Thenafter, run below command:

php artisan tinker

>>> require 'pu2s3.php';

Then just wait for it to finish.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment