BLOGS

AWS Intergration In Php

Amazon S3 Introduction:

Amazon Simple Storage Service (Amazon S3) application programming interface (API) is storage for the internet. You can use Amazon S3 to store and retrieve any amount of data at any time, from anywhere on the web. Amazon S3 supports the REST API.

Installing the AWS SDK Version 3 for PHP

  • As a dependency via Composer
  • As a prepackaged phar of the SDK
  • As a ZIP file of the SDK

Before you install AWS SDK Version 3 for PHP ensure your environment is using PHP version 5.5 or later.

 

Add AWS SDK for PHP as a dependency via Composer:

Composer is the recommended way to install the AWS SDK for PHP. Composer is a tool for PHP that manages and installs the dependencies of your project.
  • If Composer is not already in your project, download and install Composer (http://getcomposer.org/download).
  • If Composer is installed on your system, run the following in the base directory of your project to install AWS SDK for PHP as a dependency:
 
 
                       composer require aws/aws-sdk-php
 
Otherwise type this Composer command to install the latest version of the AWS SDK for PHP as a dependency.
 
     php -d memory_limit=-1 composer.phar require aws/aws-sdk-php

Add autoloader to your php scripts:

To utilize the AWS SDK for PHP in your scripts, include the autoloader in your scripts, as follows.
 
                      require ‘/path/to/vendor/autoload.php’;
 

Using Amazon S3 Select to select records:

We can now use the AWS Intergration In Php with the Amazon S3 SelectObjectContent API to select records from JSON and CSV files stored in Amazon S3.
First, we want our application to create a new Amazon S3 client for the AWS Region that our my-bucket is in. We’ll use this client to make the SelectObjectContent API calls.
 
           use Aws\S3\S3Client;
           $aws = new S3Client([
           ‘region’ => ‘us-east-1’,
           ‘version’ => ‘latest’,
           ]);
 

PutObject:

You can perform actions on a resource by calling verb-like methods on the object. You must have WRITE permissions on a bucket to add an object to it.
 
 
 
           // Create a bucket and object.
              $bucket =  $aws->s3->createBucket([
              ‘Bucket’ => ‘my-new-bucket’
               ]);
              $object = $bucket->putObject([
              ‘Key’ => ‘image.png’,
              ‘Body’ => fopen(‘/path/to/image.png’, ‘r’),
              ‘ACL’ => ‘public-read’,
              ‘ContentType’ => ‘image/png’,
              ]);
        // Delete the bucket and object.
             $object->delete();
             $bucket->delete();
 
AWS Intergration In Php
Because the resource’s identity is encapsulated within the resource object, you never have to specify it again once the object is created. This way, actions like $object->delete() do not need to require arguments.