Amazon S3 is storage for the Internet and is designed to make web-scale computing easier for developers. Amazon S3 services is used for storing and managing files dynamically. With the help of PHP we can upload/download an unlimited number of files in TB’s size without having any fear of loosing your data.
S3 provides a simple web services interface that can be used to store and retrieve any amount of data, at any time, from anywhere on the web. It gives any developer access to the same highly scalable, reliable, secure, fast, inexpensive infrastructure that Amazon uses to run its own global network of web sites. The service aims to maximize benefits of scale and to pass those benefits on to developers.
Amazon S3 is intentionally built with a minimal feature set.
- Write, read, and delete objects containing from 1 byte to 5 gigabytes of data each. The number of objects you can store is unlimited.
- Each object is stored in a bucket and retrieved via a unique, developer-assigned key.
- A bucket can be stored in one of several Regions. You can choose a Region to optimize for latency, minimize costs, or address regulatory requirements. Amazon S3 is currently available in the US Standard, EU (Ireland), US West (Northern California) and Asia Pacific (Singapore) Regions. The US Standard Region automatically routes requests to facilities in Northern Virginia or the Pacific Northwest using network maps.
- Objects stored in a Region never leave the Region unless you transfer them out. For example, objects stored in the EU (Ireland) Region never leave the EU.
- Authentication mechanisms are provided to ensure that data is kept secure from unauthorized access. Objects can be made private or public, and rights can be granted to specific users.
- Uses standards-based REST and SOAP interfaces designed to work with any Internet-development toolkit.
- Built to be flexible so that protocol or functional layers can easily be added. The default download protocol is HTTP. A BitTorrent™ protocol interface is provided to lower costs for high-scale distribution.
Getting Started with Amazon S3
Using Amazon S3 is easy. To get started you:
- Create a Bucket to store your data. You can choose a Region where your bucket and object(s) reside to optimize latency, minimize costs, or address regulatory requirements.
- Upload Objects to your Bucket. Your data is durably stored and backed by the Amazon S3 Service Level Agreement.
- Optionally, set access controls. You can grants others access your data from anywhere in the world.
You can easily and securely create buckets, upload objects, and set access controls using the AWS Management Console. The console provides a point-and-click web-based interface for accessing and managing all of your Amazon S3 resources.
Amazon S3 offers unlimited storage capacity for just pennies per gigabyte per month. Files may be uploaded and downloaded directly by your end-users, or you may access data stored on Amazon S3 via your application alone. Bandwidth rates are inexpensive, so files with high volume access can be served from Amazon S3 without breaking the bank.
S3 on PHP
Getting Started
Before we go any further, start by registering for an AWS account. All of the services we’ll be discussing require an Access Key ID and a Secret Access Key from AWS. If you don’t have these keys, you can get them quickly by visiting aws.amazon.com and following the quick and easy sign-up process. Once you get your AWS credentials, put them in a safe place. Where that will ultimately be depends on your security preferences, but I recommend a centrally-located file that is accessible only by web applications and privileged users.
Download the files covered in this article here: http://s3.killersoft.com/AWSforPHP/awsfiles.zip.
For our examples, we’ll assume that you’ve placed your credentials in /etc/aws.conf, in standard INI file format. For example:
-
; AWS credentials ; Account ID account_id = "1234567890" ; Access Key Id access_key = "06224BHAZ75910F2" ; Secret Access Key secret_key = "aIfbA2568+12TEqLDYpiqOyRULvi9"
Please take care to set appropriate permissions on this file to limit access to it. Keep these credentials safe.
class.s3.php
class.s3.php allows you to interact with Amazon’s Simple Storage Service (S3). You can add/remove buckets and keys and query information about them as well. Curl is used to enable working with large files.
Usage
The following code creates a new S3 object and displays a list of available buckets.
[php]
$s3 = new S3(“Your Amazon Key”, “Your Secret Amazon Key”);
$buckets = $s3->getBuckets();
foreach($buckets as $bucket)
echo $bucket . "<br/>";
[/php]
Members
$key : Contains your Amazon key.
$secret : Contains your Amazon secret key.
Example usage :
[php]
$s3 = new S3(‘accessKey’, ‘secretKey’);
$s3->putBucket(‘bucket’, S3::ACL_PUBLIC_READ);
$s3->putObjectFile(‘file.doc’, ‘bucket’, ‘docs/file.doc’, S3::ACL_PUBLIC_READ);
$s3->deleteObject(‘bucket’, ‘docs/file.doc’);
[/php]
[php]
//include the S3 class
if (!class_exists(‘S3’))require_once(‘S3.php’);
//AWS access info
if (!defined(‘awsAccessKey’)) define(‘awsAccessKey’, ‘CHANGETHIS’);
if (!defined(‘awsSecretKey’)) define(‘awsSecretKey’, ‘CHANGETHISTOO’);
//instantiate the class
$s3 = new S3(awsAccessKey, awsSecretKey);
//we’ll continue our script from here
[/php]