January 26, 2015 3:25 pm

AWS Service Provider for Laravel 4

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. We are going to install AWS service provider via Composer by requiring the aws/aws-sdk-php-laravel package in our projects composer.json

AWS Service Provider for Laravel 4

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

Then run a composer 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.

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

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

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

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.

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

Creating a bucket in S3 using AWS service provider.

Creating a bucket in another region in S3.

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.

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!’.

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.

We can also upload object using a stream also.

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

We can also list objects in a single bucket.

We can also download objects.

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

Author Huzoor Bux

I am Huzoor Bux from Karachi (Pakistan). I have been working as a PHP Developer from last 5+ years, and its my passion to learn new things and implement them as a practice. Basically I am a PHP developer but now days exploring more in HTML5, CSS and jQuery libraries.


Tutorial Categories:
  • vbnvbnv

    vnbnbnbn