Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Amazon S3 Cookbook (n)

You're reading from   Amazon S3 Cookbook (n) Over 30 hands-on recipes that will get you up and running with Amazon Simple Storage Service (S3) efficiently

Arrow left icon
Product type Paperback
Published in Aug 2015
Publisher Packt
ISBN-13 9781785280702
Length 280 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Naoya Hashimoto Naoya Hashimoto
Author Profile Icon Naoya Hashimoto
Naoya Hashimoto
Arrow right icon
View More author details
Toc

Table of Contents (14) Chapters Close

Preface 1. Managing Common Operations with AWS SDKs FREE CHAPTER 2. Hosting a Static Website on Amazon S3 Bucket 3. Calculating Cost with the AWS Simple Monthly Calculator 4. Deploying a Static Website with CloudFormation 5. Distributing Your Contents via CloudFront 6. Securing Resources with Bucket Policies and IAM 7. Sending Authenticated Requests with AWS SDKs 8. Protecting Data Using Server-side and Client-side Encryption 9. Enabling Cross-origin Resource Sharing 10. Managing Object Lifecycle to Lower the Cost 11. S3 Performance Optimization 12. Creating Triggers and Notifying S3 Events to Lambda Index

Learning AWS SDK for PHP and basic S3 operations with sample code

This section introduces you about how to install AWS SDK for PHP and how to create S3 buckets, put objects, get objects using the sample code, and explains how the sample code runs as well.

Getting ready

AWS SDK for PHP is a powerful tool for PHP developers to quickly build their stable applications.

To get started with AWS SDK for PHP, it is necessary to install the following on your development machine:

It is recommended to use Composer to install AWS SDK for PHP because it is much easier than getting the source code.

How to do it…

Proceed with the following steps to install the packages and run the sample application:

  1. Download the sample SDK application:
    $ git clone https://github.com/awslabs/aws-php-sample.git
    $ cd aws-php-sample/
    
  2. Run the sample application:
    $ php sample.php
    

How it works…

The sample code works as shown in the following diagram; initiating the credentials to allow access to Amazon S3, creating a bucket in a region, putting and getting objects into the bucket, and then finally deleting the objects and the bucket:

How it works…

Now, let's run the sample application and see the output of the preceding command, as shown in the following screenshot, and then follow the source code step by step:

How it works…

Now, let's examine the sample code; the path is aws-php-sample/sample.php.

The s3Client::facory method creates an AWS client and is the easiest way to get up and running:

$client = S3Client::factory();

The createBucket method creates an S3 bucket with the specified name in a region defined in the credentials file:

$result = $client->createBucket(array(
    'Bucket' => $bucket
));

The PutOjbect method uploads objects into the bucket:

$key = 'hello_world.txt';
$result = $client->putObject(array(
    'Bucket' => $bucket,
    'Key'    => $key,
    'Body'   => "Hello World!"
));

The getObject method retrieves objects from the bucket:

$result = $client->getObject(array(
    'Bucket' => $bucket,
    'Key'    => $key
));

The deleteObject method removes objects from the bucket:

$result = $client->deleteObject(array(
    'Bucket' => $bucket,
    'Key'    => $key
));

The deleteBucket method deletes the bucket:

$result = $client->deleteBucket(array(
    'Bucket' => $bucket
));

The whole sample code is as follows:

<?php
use Aws\S3\S3Client;
$client = S3Client::factory();
$bucket = uniqid("php-sdk-sample-", true);
echo "Creating bucket named {$bucket}\n";
$result = $client->createBucket(array(
    'Bucket' => $bucket
));
$client->waitUntilBucketExists(array('Bucket' => $bucket));
$key = 'hello_world.txt';
echo "Creating a new object with key {$key}\n";
$result = $client->putObject(array(
    'Bucket' => $bucket,
    'Key'    => $key,
    'Body'   => "Hello World!"
));
echo "Downloading that same object:\n";
$result = $client->getObject(array(
    'Bucket' => $bucket,
    'Key'    => $key
));
echo "\n---BEGIN---\n";
echo $result['Body'];
echo "\n---END---\n\n";
echo "Deleting object with key {$key}\n";
$result = $client->deleteObject(array(
    'Bucket' => $bucket,
    'Key'    => $key
));
echo "Deleting bucket {$bucket}\n";
$result = $client->deleteBucket(array(
    'Bucket' => $bucket
));

See also

You have been reading a chapter from
Amazon S3 Cookbook (n)
Published in: Aug 2015
Publisher: Packt
ISBN-13: 9781785280702
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime