Skip to content

Instantly share code, notes, and snippets.

@connor11528
Last active May 29, 2019 17:50
Show Gist options
  • Save connor11528/6413964c62efd18cdc0a79ed657b9488 to your computer and use it in GitHub Desktop.
Save connor11528/6413964c62efd18cdc0a79ed657b9488 to your computer and use it in GitHub Desktop.
Laravel route to upload a file to Amazon S3 bucket
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
use Illuminate\Support\Facades\Storage;
class CandidateController extends Controller
{
public function uploadResume(Request $request, User $user)
{
$candidate = auth()->user();
// Verify that authenticated user is who we're uploading the file for
if ($candidate->id != $user->id) {
abort(403, 'Forbidden');
}
// Get file from the request object
// Returns an instance of UploadedFile
// https://github.com/laravel/framework/blob/e6c8aa0e39d8f91068ad1c299546536e9f25ef63/src/Illuminate/Http/UploadedFile.php
$resume = $request->file('resume');
// Create a human readable file name for storing in the S3 bucket
$resumeFileName = $candidate->id . '_' . $candidate->first_name . '_' . $candidate->last_name . '_resume.' . $resume->getClientOriginalExtension();
// Upload file to "resumes" folder in S3 with our file name and publically accessible file visibility
$filePath = Storage::putFileAs(
'resumes',
$resume,
$resumeFileName,
[ 'visibility' => 'public' ]
);
// Store the file path with the candidate's database record
$candidate->resume_file_path = $filePath;
$candidate->save();
return response()->json($filePath);
}
}
@connor11528
Copy link
Author

Full tutorial for uploading files with Laravel and Vue.js: https://employbl.com/blog/laravel-file-upload-s3-bucket

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