I am writing an application in Lumen, to provide APIs to the frontend framework. I am struggling with file upload to S3. Steps I followed.
- Configured .env file with
AWS_ACCESS_KEY_ID=AKXXXXXXXXXXXXXXXXXX AWS_SECRET_ACCESS_KEY=nXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX AWS_DEFAULT_REGION=me-south-1 AWS_BUCKET=your_bucket_name AWS_URL=https://apigateway.me-south-1.amazonaws.com
Using composer installed AWS SDK for Laravel
composer require aws/aws-sdk-php-laravel:~3.0
Now wrote the function like this
public function upload(Request $request) { $image = $request->file('image'); $imageFileName = time() . '.' . $image->getClientOriginalExtension(); $s3 = Storage::disk('s3'); $filePath = '/venue/' . $imageFileName; $s3->put($filePath, file_get_contents($image)); return response()->json(['status' => true, 'data' => $imageFileName], Response::HTTP_OK); }
It would be really great if someone can share an article or full tutorial on how to do this.