First set your s3 bucket configuration in your .env file as below.
S3_AWS_KEY=AKIAIDTXXXHUR7ABCDEF
S3_AWS_SECRET=2XTTy0q8xxdaddffrtsfsdfdfgfhvcdsfsf
S3_AWS_REGION=us-east-2
S3_AWS_BUCKET=BUCKET_NAME
In your controller file add the following code to set the public property to the uploaded image.
if (request()->hasFile(‘image_field_name_inyour_form’)) {
$folderPath = ‘profile/thumbnailfolder/’; // Set the folder path where you want to upload on s3 bucket
$imageName = time().rand(0,999999).’.’.request()->file(‘main_image’)->getClientOriginalExtension(); // Set name for the uploaded image with extension
$imageObj = request()->file(‘main_image’); // This will retrieve the uploaded images attributes. in short the file objects.
$uploadResponse = Storage::disk(‘s3’)->put($folderPath.$imageName, file_get_contents($imageObj), ‘public’); // here if you dont set the “public” attributes, when you access the file in your browser it will display access permission denied error.
$imageNameToStoreInYourDbOrCode = $imageName;
}
Hope you like the code. Please write back for any queries.
Vinod Ram has been in Software Industry since 2006 and has experience of over 16 years in Software Development & Project Management domain specialised majorly in LAMP stack & Open Source Technology, building enterprise level Web based Application, Large Database driven and huge traffic Websites and Project Management.
He loves to write information articles and blog to share his knowledge and experience with the outside world and help people to find solution for their problems.