Configure and Retrieve S3 Information Using Laravel PHP-AWS-SDK

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:

AWS PHP SDK with Laravel

We’ll retrieve S3 bucket information using the AWS SDK in the below Laravel application.

Install the SDK via Composer

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

Add AWS Credentials to Your Environment 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 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
    ],

]

Creating an S3 Client

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;
        }
    }
}

Create a Service or Controller Method

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'));
    }
}

Update routes/api.php File

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']);

Conclusion

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.

Leave a Reply

Your email address will not be published. Required fields are marked *