Skip to content

Instantly share code, notes, and snippets.

@sohailalam2
Created March 1, 2017 11:12
Show Gist options
  • Select an option

  • Save sohailalam2/304e3d547f9cba6a8bfcdffcfc3686e6 to your computer and use it in GitHub Desktop.

Select an option

Save sohailalam2/304e3d547f9cba6a8bfcdffcfc3686e6 to your computer and use it in GitHub Desktop.

Revisions

  1. sohailalam2 created this gist Mar 1, 2017.
    72 changes: 72 additions & 0 deletions s3-test.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,72 @@
    <!--
    Setup to connect to AWS S3 in PHP

    1. Install Composer:
    Using Composer is the recommended way to install the AWS SDK for PHP.

    curl -sS https://getcomposer.org/installer | php


    2. Run the Composer command to install the latest stable version of the SDK:

    php composer.phar require aws/aws-sdk-php


    3. Require Composer's autoloader in your php file:

    require 'vendor/autoload.php';

    4. Set environment variables for AWS Id and Key:

    export AWS_ACCESS_KEY_ID="YOUR_ACCESS_ID"
    export AWS_SECRET_ACCESS_KEY="YOUR_ACCESS_KEY"
    -->

    <?php

    // Include the SDK using the Composer autoloader
    require 'vendor/autoload.php';

    // Use the us-west-2 region and latest version of each client.
    $sharedConfig = [
    'region' => 'us-west-2',
    'version' => 'latest'
    ];

    // Create an SDK class used to share configuration across clients.
    $sdk = new Aws\Sdk($sharedConfig);

    // Use an Aws\Sdk class to create the S3Client object.
    $s3Client = $sdk->createS3();

    // Use an Aws\Sdk class to create the S3Client object.
    $s3 = $sdk->createS3();

    // using promises for asyncronous requests
    $promise = $s3Client->listBucketsAsync();
    $promise
    ->then(function ($result) {
    echo 'Got a result: ' . var_export($result, true);
    })
    ->otherwise(function ($reason) {
    echo 'Encountered an error: ' . $reason->getMessage();
    });

    // Send a PutObject request and get the result object.
    $result = $s3Client->putObject([
    'Bucket' => 'my-bucket',
    'Key' => 'my-key',
    'Body' => 'this is the body!'
    ]);

    // Print the body of the result by indexing into the result object.
    echo $result['Body'];

    // Download the contents of the object.
    $result = $s3Client->getObject([
    'Bucket' => 'my-bucket',
    'Key' => 'my-key'
    ]);

    // Print the body of the result by indexing into the result object.
    echo $result['Body'];