here’s a step-by-step guide to uploading files to an AWS S3 bucket in a Laravel application using the AWS SDK for PHP
Step 1: Set Up AWS Credentials
Make sure you have your AWS credentials (Access Key ID and Secret Access Key) available. You’ll also need the AWS region and the name of the S3 bucket where you want to upload the files.
Step 2: Install AWS SDK for PHP via Composer
Run the following command in your Laravel project directory to install the AWS SDK for PHP
composer require aws/aws-sdk-php
Step 3: Configure AWS Credentials
Add your AWS credentials and other S3 configuration settings to the .env
file in your Laravel project. Open the .env
file and add the following lines
AWS_ACCESS_KEY_ID=your-access-key-id
AWS_SECRET_ACCESS_KEY=your-secret-access-key
AWS_DEFAULT_REGION=your-aws-region
AWS_BUCKET=your-bucket-name
Step 4: Create a Controller or Use Existing Controller
You can create a new controller or use an existing one to handle file uploads. For example, let’s create a controller named S3Controller
using the Artisan command
php artisan make:controller S3Controller
Step 5: Implement File Upload Logic
Inside the S3Controller
, write a method to handle file uploads to S3. Here’s an example of how the code might look
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Aws\S3\S3Client;
class S3Controller extends Controller
{
public function upload(Request $request)
{
// Validate the uploaded file (if needed)
$request->validate([
'file' => 'required|file',
]);
// Get the uploaded file
$file = $request->file('file');
// Initialize AWS S3 client
$s3 = new S3Client([
'version' => 'latest',
'region' => config('aws.default_region'),
'credentials' => [
'key' => config('aws.access_key_id'),
'secret' => config('aws.secret_access_key'),
],
]);
// Generate a unique name for the file in S3 bucket
$fileName = time() . '_' . $file->getClientOriginalName();
// Upload file to S3 bucket
$result = $s3->putObject([
'Bucket' => config('aws.bucket'),
'Key' => $fileName,
'Body' => fopen($file->getPathname(), 'rb'),
'ACL' => 'public-read', // Set ACL permissions for the uploaded file
]);
// Get the URL of the uploaded file
$fileUrl = $result['ObjectURL'];
// You can store this URL or perform any other actions needed
return response()->json(['url' => $fileUrl]);
}
}
Step 6: Route Setup
Define a route to handle file uploads in your routes/web.php
file
use App\Http\Controllers\S3Controller;
Route::post('/upload', [S3Controller::class, 'upload']);
Step 7: Testing and Verification
To test the file upload functionality:
- Run your Laravel application (
php artisan serve
or use your server). - Create a form in your view to upload a file.
- Submit the form with a file attached.
- Verify the file gets uploaded to your S3 bucket by checking the bucket directly or by retrieving the file URL returned by the controller.
Ensure you can access the uploaded file via the provided URL and that it appears in your S3 bucket.
Remember to handle error cases appropriately and secure your file upload endpoint as needed for your application.
Make sure to replace 'your-access-key-id'
, 'your-secret-access-key'
, 'your-aws-region'
, and 'your-bucket-name'
with your actual AWS credentials and S3 bucket information in your .env
file and code.