This tutorial helps combine a PHP SDK with Laravel. We’ll set up aws-php-sdk into laravel software and entry all aws providers utilizing technique.
Let’s combine aws-php-sdk in Laravel by following these basic steps:
AWS PHP SDK with Laravel
We’ll retrieve S3 bucket data utilizing the AWS SDK within the beneath Laravel software.
Set up the SDK through Composer
We’ll use PHP Composer to create the applying. Composer is PHP’s dependency supervisor that helps set up packages. To put in a bundle, you need to use the composer require command or add it to your composer.json file.
composer require aws/aws-sdk-php
Add AWS Credentials to Your Surroundings File
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
Configure AWS PHP-SDK in Laravel
Laravel comes with built-in assist for AWS through the filesystems configuration. Let’s create a config/aws-services.php file and arrange 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
],
]
Creating an S3 Shopper
We will create an S3 consumer utilizing the AWS SDK. That helps to straight work together with the S3 service by aws-ph-sdk. You’ll be able to name the s3 service technique into the service technique:
namespace AppServices;
use AwsS3S3Client;
use AwsExceptionAwsException;
use IlluminateSupportFacadesConfig;
class S3Service
{
protected $s3Client;
public operate __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 operate getBucketLocation()
{
strive {
$consequence = $this->s3Client->getBucketLocation([
'Bucket' => Config::get('filesystems.disks.s3.bucket'),
]);
return $result->get('LocationConstraint');
} catch (AwsException $e) {
// Deal with the error or return null/false
return null;
}
}
}
Create a Service or Controller Methodology
Now you can use this service in a controller. That technique will uncovered in api.php file to entry it.
namespace AppServices;
use AwsExceptionAwsException;
use IlluminateSupportFacadesConfig;
use AwsS3S3Client;
class S3Controller extends Controller
{
protected $s3Service;
public operate __construct(S3Service $s3Service)
{
$this->s3Service = $s3Service;
}
public operate showBucketLocation()
{
$location = $this->s3Service->getBucketLocation();
return view('s3.bucket-location', compact('location'));
}
}
Replace routes/api.php File
Let’s make an entry within the routes/api.php file that may deal with the request to retrieve S3 bucket data:
Route::get('/s3/bucket-location', [S3Controller::class, 'getBucketLocation']);
Conclusion
We’ve put in and configured aws-php-sdk in laravel software. We’ve uncovered an API endpoint in your Laravel software that may be referred to as to retrieve S3 bucket data.

