Integration of S3 with PHP application
Amazon S3 is an excellent storage service that helps by providing media file storage a capacity with highly scalable, reliable, secure, fast, inexpensive infrastructure that Amazon uses to run its own global network of web sites. This is simply a very good solution for your web application when it deals with large number of image/music/video files.
We have recently integrated S3 bucket with a PHP application based on cakePHP framework. We found it very simple. The following steps help the integration(development mode).
# Create access key and secret access key by registering https://portal.aws.amazon.com/gp/aws/securityCredentials
# Download SDK for PHP integration from https://portal.aws.amazon.com/gp/aws/securityCredentials
# Edit the config file with your developer access key and secret access key
# Create amazon s3 object by using the command $s3 = new AmazonS3();
# Create bucket
$create_bucket_response = $s3->create_bucket($bucket, AmazonS3::REGION_US_W1);
// Provided that the bucket was created successfully…
if ($create_bucket_response->isOK())
{
/* Since AWS follows an “eventual consistency” model, sleep and poll
until the bucket is available. */
$exists = $s3->if_bucket_exists($bucket);
while (!$exists)
{
// Not yet? Sleep for 1 second, then check again
sleep(1);
$exists = $s3->if_bucket_exists($bucket);
}
/*
Get a list of files to upload. We’ll use some helper functions we’ve
defined below. This assumes that you have a directory called “test_files”
that actually contains some files you want to upload.
*/
$list_of_files = filter_file_list(glob(‘./test_files/*’));
// Prepare to hold the individual filenames
$individual_filenames = array();
$create_bucket_response = $s3->create_bucket($bucket, AmazonS3::REGION_US_W1); // Provided that the bucket was created successfully… if ($create_bucket_response->isOK()) { /* Since AWS follows an “eventual consistency” model, sleep and poll until the bucket is available. */ $exists = $s3->if_bucket_exists($bucket); while (!$exists) { // Not yet? Sleep for 1 second, then check again sleep(1); $exists = $s3->if_bucket_exists($bucket); } /* Get a list of files to upload. We’ll use some helper functions we’ve defined below. This assumes that you have a directory called “test_files” that actually contains some files you want to upload. */ $list_of_files = filter_file_list(glob(‘./test_files/*’)); // Prepare to hold the individual filenames $individual_filenames = array();
Make sure you give a unique name to the bucket while creating it.
# The following method helps you to see if the bucket is created successfully $create_bucket_response->isOK()
# Upload your files to the bucket using
$individual_filenames = array();
// Loop over the list, referring to a single file at a time
foreach ($list_of_files as $file)
{
// Grab only the filename part of the path
$filename = explode(DIRECTORY_SEPARATOR, $file);
$filename = array_pop($filename);
// Store the filename for later use
$individual_filenames[] = $filename;
/* Prepare to upload the file to our new S3 bucket. Add this
request to a queue that we won’t execute quite yet. */
$s3->batch()->create_object($bucket, $filename, array(
‘fileUpload’ => $file
));
}
/* Execute our queue of batched requests. This may take a few seconds to a
few minutes depending on the size of the files and how fast your upload
speeds are. */
$file_upload_response = $s3->batch()->send();
/* Since a batch of requests will return multiple responses, let’s
make sure they ALL came back successfully using `areOK()` (singular
responses use `isOK()`). */
if ($file_upload_response->areOK())
{
// Loop through the individual filenames
foreach ($individual_filenames as $filename)
{
/* Display a URL for each of the files we uploaded. Since uploads default to
private (you can choose to override this setting when uploading), we’ll
pre-authenticate the file URL for the next 5 minutes. */
echo $s3->get_object_url($bucket, $filename, ’5 minutes’) . PHP_EOL . PHP_EOL;
}
}
Give it a try. The Source: http://www.appsbee.com/blogs/2012/11/11/integration-of-s3-with-php-application/









