KodeInfo | Learning resources for web and mobile development

AWS Service Provider for Laravel 4

January 5th, 2015 11:53:14 by Imran Iqbal Comments(0) - Views(1132)
Tags :

In this tutorial we are going to learn how to use AWS service provider package built for laravel 4 and how to save a document into S3 bucket. We are going to include AWS SDK for PHP to the laravel as a package using composer.

Installation

As i said we are going to install AWS service provider via Composer by requiring the aws/aws-sdk-php-laravel package in our projects composer.json

{
	"name": "laravel/laravel",
	"description": "The Laravel Framework.",
	"keywords": ["framework", "laravel"],
	"license": "MIT",
	"type": "project",
	"require": {
		"laravel/framework": "4.2.*",
		"intervention/image": "dev-master",
		 "aws/aws-sdk-php-laravel": "1.*"
	},
	"autoload": {
		"classmap": [
			"app/commands",
			"app/controllers",
			"app/models",
			"app/database/migrations",
			"app/database/seeds",
			"app/tests/TestCase.php"
		]
	},
	"scripts": {
		"post-install-cmd": [
			"php artisan clear-compiled",
			"php artisan optimize"
		],
		"post-update-cmd": [
			"php artisan clear-compiled",
			"php artisan optimize"
		],
		"post-create-project-cmd": [
			"php artisan key:generate"
		]
	},
	"config": {
		"preferred-install": "dist"
	},
	"minimum-stability": "stable"
}

Then run a composer update

php composer.phar update

Configuration

To use the AWS Service provider, we must register the provider when bootstrapping our laravel application. To publish the package configuration using Artisan, we have to use this command.

php artisan config:publish aws/aws-sdk-php-laravel

We have to update our settings in the generated app/config/packages/aws/aws-sdk-php-laravel configuration file.

return array(
    'key'         => 'YOUR_AWS_ACCESS_KEY_ID',
    'secret'      => 'YOUR_AWS_SECRET_KEY',
    'region'      => 'us-east-1',
    'config_file' => null,
);

We have to providers key in our app/config/app.php  and register the AWS service provider.

'providers' => array(
        // ...
        'Aws\Laravel\AwsServiceProvider',
    )

 

Next we have to find our aliases key in our app/config/app.php and add the AWS facade alias.

 'aliases' => array(
        // ...
        'AWS' => 'Aws\Laravel\AwsFacade',
    )

Usage

In order to use the AWS SDK for PHP within our app, we 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'        => 'YOUR_OBJECT_KEY',
    'SourceFile' => '/the/path/to/the/file/you/are/uploading.ext',
));

If the AWS facade is registered within the aliases section of the application configuration, we can also use the following technique.

$s3 = AWS::get('s3');
$s3->putObject(array(
    'Bucket'     => 'YOUR_BUCKET',
    'Key'        => 'YOUR_OBJECT_KEY',
    'SourceFile' => '/the/path/to/the/file/you/are/uploading.ext',
));

Creating a bucket in S3 using AWS service provider.

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

$s3->createBucket(array('bucket' => 'myBucket'));

Creating a bucket in another region in S3.

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

$s3->createBucket(array(
           'bucket' => $bucket, 
           'LocationConstraint' => 'us-west-2'
));

Now that we have created a bucket, let's force our application to wait until the bucket exists. This can be done easily using a waiter. The following snippet of code will poll the bucket until it exists or the maximum number of polling attempts are completed.

$client->waitUntil('BucketExists',array('Bucket'=>$bucket));

Now that we have created a bucket, let's put some data in it. The following example creates an object in our bucket called data.txt that contains 'Hello!'.

$s3->putObject(array(
         'Bucket' => $bucket, 
         'Key' => 'data.txt', 
         'Body' => 'Hello!'
));

The AWS SDK for PHP will attempt to automatically determine the most appropriate Content-Type header user to store the object. If you are using a less common file extension and your Content-Type header is not added automatically, You can add a Content-Type header by passing a Content-Type option to the operation.

We can also poll the object until it is accessible using waitUntil method of AWS SDk like below.

$s3->waitUntil('ObjectExists',  array(
        'bucket' => $bucket, 
        'Key' => 'data_from_file.txt'
));

We can also upload object using a stream also.

$s3->putObject(array(
         'bucket' => $bucket, 
         'Key' =>data_from_stream.txt', 
         'Body' => fopen('$pathToFile, 'r+')
));

We can also list all the buckets in our S3 like below.

$s3->listBucket();

We can also list objects in a single bucket.

$s3->getIterator('ListObjects', array('bucket' => $bucket));

We can also download objects.

$s3->getObject('array(
         'bucket' => $bucket, 
         'Key' => 'data.txt'
));

For more reference about AWS SDK please check below links.

I hope this will help in some way for the people who works AWS SDK for PHP in laravel application. Looking forward to share more these kind of stuff.

Thanks

Kode Info

 

Author

  • Imran Iqbal
    Imran Iqbal

    Imran is a web developer and consultant from India. He is the founder of KodeInfo, the PHP and Laravel Community . In the meantime he follows other projects, works as a freelance backend consultant for PHP applications and studies IT Engineering . He loves to learn new things, not only about PHP or development but everything.

Related

WHY USE A FRAMEWORK OVER PLAIN PHP

WHY USE A FRAMEWORK OVER PLAIN PHP
read more

GETTING STARTED WITH LARAVEL

GETTING STARTED WITH LARAVEL
read more

UNDERSTANDING LARAVEL STRUCTURE

UNDERSTANDING LARAVEL STRUCTURE
read more

UNDERSTANDING LARAVEL ROUTES

UNDERSTANDING LARAVEL ROUTES
read more

comments powered by Disqus