A simple Laravel 4 service provider for including the AWS SDK for PHP.
The AWS Service Provider can be installed via Composer by requiring the
aws/aws-sdk-php-laravel
package in your project's composer.json
.
{
"require": {
"aws/aws-sdk-php-laravel": "1.*"
}
}
To use the AWS Service Provider, you must register the provider when bootstrapping your Laravel application. There are essentially two ways to do this.
Create a new app/config/aws.php
configuration file with the following options:
return array(
'key' => '<your-aws-access-key-id>',
'secret' => '<your-aws-secret-access-key>',
'region' => Aws\Common\Enum\Region::US_WEST_2,
);
Find the providers
key in app/config/app.php
and register the AWS Service Provider.
'providers' => array(
// ...
'Aws\Laravel\AwsServiceProvider',
)
Find the aliases
key in app/config/app.php
and add the AWS facade alias.
'aliases' => array(
// ...
'AWS' => 'Aws\Laravel\AwsFacade',
)
You can also register the provider and configuration options at runtime. This could be done in your global bootstrapping
process in app/start/global.php
.
use Aws\Common\Enum\Region;
use Aws\Laravel\AwsServiceProvider;
use Illuminate\Foundation\Application;
// Instantiate a new application. This is normally done by the Laravel framework and the instance is available in
// `app/start/global.php` for you to use.
$app = new Application;
// Register the AWS service provider and provide your configuration
$app->register(new AwsServiceProvider($app), array(
'config' => array(
'aws' => array(
'key' => '<your-aws-access-key-id>',
'secret' => '<your-aws-secret-access-key>',
'region' => Region::US_WEST_2,
),
),
));
You can alternatively specify the path to an AWS config file (see AWS SDK for PHP for details about how to format this type of file).
$app->register(new AwsServiceProvider($app), array('config' => array('aws' => '/path/to/aws/config/file.php')));
Either way, the value of $app['config']['aws']
is passed directly into Aws\Common\Aws::factory()
.
In order to use the SDK from within your app, you need to retrieve it from the Laravel IoC Container. The following example uses the Amazon S3 client to upload a file.
$s3 = App::make('aws')->get('s3');
$s3->putObject(array(
'Bucket' => '<your-bucket>',
'Key' => '<the-name-of-your-object>',
'SourceFile' => '/path/to/the/file/you/are/uploading.ext',
));
If the AWS Facade is registered within the aliases
section of the application configuration, you can use
the following more expressive method.
$s3 = AWS::get('s3');
$s3->putObject(array(
'Bucket' => '<your-bucket>',
'Key' => '<the-name-of-your-object>',
'SourceFile' => '/path/to/the/file/you/are/uploading.ext',
));