aws-sdk-php-laravel

repository·master·Indexed 23 days ago

https://github.com/aws/aws-sdk-php-laravel

A Laravel service provider that integrates the official AWS SDK for PHP into Laravel (9-13) and Lumen applications. It provides access to AWS services via the service container and the AWS facade, allowing developers to instantiate service clients using the createClient method.

Tokens
981
Snippets
3
Records
6
Agent score
83%

What's inside aws-sdk-php-laravel

  1. Install the AWS Service Provider

    master

    Install the package via Composer by adding aws/aws-sdk-php-laravel to your composer.json file and running composer update.

    Registering the Provider

    For Laravel

    Add Aws\Laravel\AwsServiceProvider::class to the providers array in config/app.php.

    To use the AWS facade, add Aws\Laravel\AwsFacade::class to the aliases array in config/app.php.

    For Lumen

    Register the provider in bootstrap/app.php using $app->register().

    {
        "require": {
            "aws/aws-sdk-php-laravel": "~3.0"
        }
    }
    // Laravel: config/app.php
    'providers' => array(
        // ...
        Aws\Laravel\AwsServiceProvider::class,
    )
    
    'aliases' => array(
        // ...
        'AWS' => Aws\Laravel\AwsFacade::class,
    )
    // Lumen: bootstrap/app.php
    $app->register(Aws\Laravel\AwsServiceProvider::class);
  2. Configure AWS credentials and region

    master

    The package automatically uses the following environment variables for configuration:

    • AWS_ACCESS_KEY_ID
    • AWS_SECRET_ACCESS_KEY
    • AWS_REGION (defaults to us-east-1)

    To customize settings or override specific services, publish the configuration file using Artisan:

    php artisan vendor:publish --provider="Aws\Laravel\AwsServiceProvider"

    This creates config/aws.php. You can define service-specific overrides (e.g., for Ses) within this file. If you prefer to use the AWS SDK default credential chain, you can remove the credentials key from the config file.

    php artisan vendor:publish  --provider="Aws\Laravel\AwsServiceProvider"
  3. Publish the AWS configuration file

    master

    To customize your AWS configuration, you can publish the configuration file to your application's config directory. This is done using the aws-config tag.

    Run the following Artisan command to publish the file:

    php artisan vendor:publish --tag=aws-config

    This will copy the configuration template to config/aws.php.

  4. Use the AWS SDK in Laravel and Lumen

    master

    You can interact with AWS services by retrieving the AWS instance from the service container and calling createClient($serviceName).

    Laravel (Service Container)

    $s3 = App::make('aws')->createClient('s3');

    Laravel (Facade)

    If you registered the AWS alias in config/app.php:

    $s3 = AWS::createClient('s3');

    Lumen

    $s3 = app('aws')->createClient('s3');
    // Example: Uploading a file to S3 in Laravel
    $s3 = AWS::createClient('s3');
    $s3->putObject(array(
        'Bucket'     => 'YOUR_BUCKET',
        'Key'        => 'YOUR_OBJECT_KEY',
        'SourceFile' => '/the/path/to/the/file/you/are/uploading.ext',
    ));
  5. Register the AWS SDK in the Laravel container

    master

    The AwsServiceProvider registers the AWS SDK as a singleton in the Laravel service container. You can resolve the AWS SDK instance using the aws binding or the Aws\Sdk alias. This allows you to inject the SDK into your classes or resolve it via the app() helper.

    When resolved, the SDK is instantiated using the configuration found under the aws key in your application's configuration files.

  6. Access AWS clients via the AwsFacade

    master

    The Aws Laravel\ AwsFacade provides a static interface to the AWS service builder. You can use it to instantiate specific AWS service clients (such as S3, DynamoDB, or EC2) by calling the createClient method. This method returns an instance implementing Aws\AwsClientInterface.