This tutorial helps integrate a PHP SDK with Laravel. We’ll install aws-php-sdk into laravel application and access all aws services using method.
Let’s integrate aws-php-sdk in Laravel by following these general steps:
We’ll retrieve S3 bucket information using the AWS SDK in the below Laravel application.
We’ll use PHP Composer to create the application. Composer is PHP’s dependency manager that helps install packages. To install a package, you can use the composer require command or add it to your composer.json
file.
composer require aws/aws-sdk-php
Please add your AWS credentials to the .env
file:
AWS_ACCESS_KEY_ID=your_access_key_id AWS_SECRET_ACCESS_KEY=your_secret_access_key AWS_DEFAULT_REGION=us-east-1 AWS_BUCKET=your_bucket_name
Laravel comes with built-in support for AWS via the filesystems configuration. Let’s create a config/aws-services.php
file and set up the S3 filesystem driver into this file.
// config/aws-services.php 'disks' => [ 's3' => [ 'driver' => 's3', 'key' => env('AWS_ACCESS_KEY_ID'), 'secret' => env('AWS_SECRET_ACCESS_KEY'), 'region' => env('AWS_DEFAULT_REGION'), 'bucket' => env('AWS_BUCKET'), 'url' => env('AWS_URL'), 'endpoint' => env('AWS_ENDPOINT'), // Optional ], ]
We can create an S3 client using the AWS SDK. That helps to directly interact with the S3 service by aws-ph-sdk. You can call the s3
service method into the service method:
namespace App\Services; use Aws\S3\S3Client; use Aws\Exception\AwsException; use Illuminate\Support\Facades\Config; class S3Service { protected $s3Client; public function __construct() { $this->s3Client = new S3Client([ 'region' => Config::get('filesystems.disks.s3.region'), 'version' => 'latest', 'credentials' => [ 'key' => Config::get('filesystems.disks.s3.key'), 'secret' => Config::get('filesystems.disks.s3.secret'), ], ]); } public function getBucketLocation() { try { $result = $this->s3Client->getBucketLocation([ 'Bucket' => Config::get('filesystems.disks.s3.bucket'), ]); return $result->get('LocationConstraint'); } catch (AwsException $e) { // Handle the error or return null/false return null; } } }
You can now use this service in a controller. That method will exposed in api.php
file to access it.
namespace App\Services; use Aws\Exception\AwsException; use Illuminate\Support\Facades\Config; use Aws\S3\S3Client; class S3Controller extends Controller { protected $s3Service; public function __construct(S3Service $s3Service) { $this->s3Service = $s3Service; } public function showBucketLocation() { $location = $this->s3Service->getBucketLocation(); return view('s3.bucket-location', compact('location')); } }
Let’s make an entry in the routes/api.php
file that will handle the request to retrieve S3 bucket information:
Route::get('/s3/bucket-location', [S3Controller::class, 'getBucketLocation']);
We have installed and configured aws-php-sdk in laravel application. We have exposed an API endpoint in your Laravel application that can be called to retrieve S3 bucket information.
in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More
This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More
We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More
in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More
I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More
in this Laravel tutorial, I will explore common practices for using the Laravel Blade template with examples. Blade is a… Read More