DreamObjects with AWSSDkforPHP 2, working examples
February 07, 2014
I was trying out the new PHP SDK for AWS and relearning the methods. While it works well on AWS I was a bit confused with DHO documentation and AWS docs. I wanted to use the SDK but with DHO.
There are advantages to the new library which you can explore. Some of the tasks we programmed manually are already done for you in the library using latest stuff in PHP 5.3 like composer, guzzle etc libraries and tools
After a couple of trial and error in the values I got things working. Here are the samples in case it helps someone else.
AWS Docs: http://docs.aws.amazon.com/aws-sdk-php/guide/latest/quick-start.html
DHO Docs: http://docs.dreamobjects.net/s3-examples/php2.html
require('vendor/autoload.php');use AwsS3S3Client;use AwsS3ExceptionS3Exception;use AwsS3EnumCannedAcl;// Instantiate the S3 client with your AWS credentials$s3 = S3Client::factory(array('key' => 'from DH panel','secret' => 'from DH panel','base\_url' => 'https://objects.dreamhost.com',));$blist = $s3->listBuckets();echo " Buckets belonging to " . $blist\['Owner'\]\['ID'\] . ":";foreach ($blist\['Buckets'\] as $b) {echo "{$b\['Name'\]} {$b\['CreationDate'\]}";}try {$result = $s3->putObject(array('Bucket' => '<mybucket>','Key' => 'data\_from\_file.txt','SourceFile' => './test.txt','ACL' => CannedAcl::PUBLIC\_READ,));}catch (Exception $e) {echo "There was an error uploading the file.";}```Uploading entire directory but new objects only. It's not perfect when used with DHO. sometimes I get missing folders when trying multiple folders. Start with the class instance as above and the following code shows how to upload differences from the directory to the target. The difference part does work well. Tested with 2000 files and 4 folders only 1 level deep.```jsxtry {$dir = 'folder1/';$bucket = '<muhbuckit>';$keyPrefix = 'myprefix/';$s3->uploadDirectory($dir, $bucket, $keyPrefix, array('params' => array('ACL' => 'public-read'),'concurrency' => 20,'debug' => true));}catch (S3Exception $e) {echo "There was an error uploading the file.";}
The concurrency works well on DHO as well. If you encounter issues with setting up the SDK do post and I will try to assist. With composer it’s quite easy but for old timers like me it might take awhile to wrap your head around on how easy it is.  I’ll post more samples as and when I write and use them.