Site icon Silva Tech Solutions LLC

AWS SDK for PHP 2 – Example Program

UPDATE: 5/9/2013:
Major kudos to the Amazon SDK PHP team for contacting us and actually listening to our concerns/issues of their SDK. A standalone download of the library is in the works and a lot of their documentation will be updated with newer examples. Kudos to the entire team and their tireless efforts of helping folks about on both the Amazon forums and StackOverflow. Great work!

UPDATE: 3/25/2013:

Make sure you have the latest version of the SDK from PEAR. We were using 2.1.0, which apparently had several bugs related to socket connection timeouts and file name issues (percent signs mixed with spaces, mixed with UTF-8 weirdness). Upgrading to the latest AWS SDK for PHP (at this time 2.2.1), fixed everything. If you are on Ubuntu 10.04, this is going to be a bit rough. (See comments about PHP version upgrades here: http://www.zalexblog.com/2011/01/07/installing-php-5-3-3-on-ubuntu-10-04/)

Recently we took on a project that involved integrating an existing portal framework to its long-term storage system into the cloud. We chose to use Amazon’s S3 SDK for PHP (Version 2), to make sure we are able to expand the code to perhaps use other AWS services in the future (Glacier etc).

The install for the new system was NOT easy to do. The libraries could not easily just be downloaded/installed, but rather had to be installed via PEAR. For portability, we really do not liking having to install modules through PEAR unless absolutely necessary. In this case, it was required.

On our UBUNTU server, we had to upgrade to the latest version of PEAR, install PHP5 Curl, and then finally install the PHP SDK from the AWS Pear channel.

pear upgrade pear
sudo apt-get install php5-curl
pear -D auto_discover=1 install pear.amazonwebservices.com/sdk

pear upgrade pear
sudo apt-get install php5-curl
pear -D auto_discover=1 install pear.amazonwebservices.com/sdk

After that was done, we set about connecting to our existing S3 account and checking out the official documentation. For one reason or the other, we found the new V2 documentation lacking in the “example” department and Google turned up very little, since a lot of folks are still using the old version of the sdk.

Listed below are two very simple scripts from the lessons learned on the project. This shows how to check the existing of a file and download it, as well asupload a file that exists in a complicated folder structure on S3.

Obviously, our final code has a significant amount more documentation and/or exception traps, but this should provide a simple model for anyone starting out with AWS services via PHP.

Check and Download an S3 file using V2 of the AWS SDK for PHP

<?
error_reporting(-1);
require 'AWSSDKforPHP/aws.phar';
use Aws\S3\S3Client;
 
$bucket="Mahbucket";
$file='/startfolder/subfolderagain/mahfile.pdf';
 
// Create an array of configuration options
	$config = array(
		'key'    => 'superhappykey',
		'secret' => 'secretyshooshkey'
	);
 
//create our new S3 v2 php factory
	$s3 = S3Client::factory($config);
 
//Check and see if our file exists?
	if($s3->doesObjectExist($bucket,$file)):
		//Now grab our file.
			$obj= $s3->getObject(
				array(
					'Bucket' => $bucket,
					'Key' => $file
				)
			);
		//get our file Content type (if set when uploaded to S3)
			$ctype=$obj->get('ContentType');
		//get our file data itself
			$data=$obj->get('Body');
		//....?  Profit
	else:
		//Does not exist.  Do something
	endif;
 
?>

<?
error_reporting(-1);
require ‘AWSSDKforPHP/aws.phar’;
use Aws\S3\S3Client;
$bucket=”Mahbucket”;
$file=’/startfolder/subfolderagain/mahfile.pdf’;
// Create an array of configuration options
$config = array(
‘key’ => ‘superhappykey’,
‘secret’ => ‘secretyshooshkey’
);
//create our new S3 v2 php factory
$s3 = S3Client::factory($config);
//Check and see if our file exists?
if($s3->doesObjectExist($bucket,$file)):
//Now grab our file.
$obj= $s3->getObject(
array(
‘Bucket’ => $bucket,
‘Key’ => $file
)
);
//get our file Content type (if set when uploaded to S3)
$ctype=$obj->get(‘ContentType’);
//get our file data itself
$data=$obj->get(‘Body’);
//….? Profit
else:
//Does not exist. Do something
endif;
?>

Uploading a file to S3 using V2 of the AWS SDK for PHP

<!--?php 
 
require 'AWSSDKforPHP/aws.phar';
 
error_reporting(-1);
use Aws\S3\S3Client;
use Guzzle\Http\EntityBody;
 
$bucket="Mahbucket";
//Where are we going to put the file?  It may look like it has folders, but S3 treats it like a big honkin filename
	$s3filelocation='/startfolder/subfolderagain/mahfile.pdf';
	$localfile=getcwd()."/scan.pdf"; //some local file you want to upload.
 
// Create an array of configuration options
	$config = array(
		'key'    =--> 'superhappykey',
		'secret' =&gt; 'secretyshooshkey'
	);
 
//check if local file exists
if(file_exists($localfile)):
	//create our new S3 v2 php factory
		$s3 = S3Client::factory($config);
 
	//Now upload it and enjoy
		$obj= $s3-&gt;putObject(array(
			'Bucket' =&gt; $bucket,
			'Key'    =&gt; $remotefile,
			'Body'   =&gt; EntityBody::factory(fopen($localfile, 'r'))
		));
 
endif;
?&gt;

<!–?php
require ‘AWSSDKforPHP/aws.phar’;
error_reporting(-1);
use Aws\S3\S3Client;
use Guzzle\Http\EntityBody;
$bucket=”Mahbucket”;
//Where are we going to put the file? It may look like it has folders, but S3 treats it like a big honkin filename
$s3filelocation=’/startfolder/subfolderagain/mahfile.pdf’;
$localfile=getcwd().”/scan.pdf”; //some local file you want to upload.
// Create an array of configuration options
$config = array(
‘key’ =–> ‘superhappykey’,
‘secret’ =&gt; ‘secretyshooshkey’
);
//check if local file exists
if(file_exists($localfile)):
//create our new S3 v2 php factory
$s3 = S3Client::factory($config);
//Now upload it and enjoy
$obj= $s3-&gt;putObject(array(
‘Bucket’ =&gt; $bucket,
‘Key’ =&gt; $remotefile,
‘Body’ =&gt; EntityBody::factory(fopen($localfile, ‘r’))
));
endif;
?&gt;

Enjoy, and don’t hesitate to CONTACT US should you have any complicated cloud integration needs.

Exit mobile version