Skip to content

Instantly share code, notes, and snippets.

@sohailalam2
Created March 1, 2017 11:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sohailalam2/304e3d547f9cba6a8bfcdffcfc3686e6 to your computer and use it in GitHub Desktop.
Save sohailalam2/304e3d547f9cba6a8bfcdffcfc3686e6 to your computer and use it in GitHub Desktop.
AWS S3 using PHP
<!--
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'];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment