Laravel

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:

Related Post
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.

Recent Posts

What is the Purpose of php_eol in PHP?

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

7 months ago

Laravel Table Relationship Methods With Example

This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More

7 months ago

Exploring the Power of Laravel Eloquent Join?

We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More

7 months ago

Quick and Easy Installation of Laravel Valet

in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More

8 months ago

What is Laravel Soft Delete and How Does it Work?

I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More

8 months ago

Common Practices for Laravel Blade Template

in this Laravel tutorial, I will explore common practices for using the Laravel Blade template with examples. Blade is a… Read More

8 months ago

Categories