Skip to content

Instantly share code, notes, and snippets.

@stevenmc
Created January 24, 2013 16:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stevenmc/4624112 to your computer and use it in GitHub Desktop.
Save stevenmc/4624112 to your computer and use it in GitHub Desktop.
CakePHP implementation of Amazon AWS SDK For PHP 2 to implement uploading a file to S3 bucket, listing the content, and deleting items.
<?php
/*
This is a fully working Class to upload, list and delete items from an Amazon S3 bucket.
Add your Config::write/Config::read variables as appropriate. Presumed region: US_EAST_1
Download the most recent cacert.pem from http://curl.haxx.se/docs/caextract.html
To install on a new machine, create a file called composer.json and add this:
{
"require": {
"aws/aws-sdk-php": "*"
}
}
Then run php command line commands:
curl -s "http://getcomposer.org/installer" | php
php composer.phar install
Add the generated files into your Vendors folder in a sub-folder called "amazonaws"
Docs:
http://docs.aws.amazon.com/awssdkdocsphp2/latest/gettingstartedguide/sdk-php2-installing-the-sdk.html
http://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectDELETE.html
Use:
App::import('Vendor', 'amazonaws', array('file' => 'amazonaws/s3.class.php'));
$s3 = new AWS_S3;
$s3->list_objects();
$s3->upload("path/to/file.jpg");
$s3->delete("path/to/file.jpg");
*/
require dirname(__FILE__) .DS.'autoload.php';
use Aws\Common\Aws;
use Aws\Common\Enum\Region;
use Aws\S3\Enum\CannedAcl;
use Aws\S3\Exception\S3Exception;
class AWS_S3{
private $_s3 = null;
public function __construct(){
$aws = Aws::factory(array(
'key' => Configure::read('AWS.key'),
'secret' => Configure::read('AWS.secret'),
'region' => Region::US_EAST_1,
'ssl.certificate_authority' => dirname(__FILE__) .DS. 'cacert.pem'
));
$this->_s3 = $aws->get('s3');
}
public function upload($file_name, $full_path){
$s3 = $this->_s3;
try {
$s3->putObject(array(
'Bucket' => Configure::read('AWS.bucket'),
'Key' => $file_name,
'Body' => fopen($full_path, 'r'),
'ACL' => CannedAcl::PUBLIC_READ,
'CAfile' => 'cacert.pem',
'ssl.CURLOPT_CAINFO' => 'cacert.pem',
'ssl.certificate_authority' => dirname(__FILE__) .DS. 'cacert.pem'
));
} catch (S3Exception $e) {
echo "The file was not uploaded.\n";
}
}
public function delete($file_name){
$s3 = $this->_s3;
try{
$s3->deleteObject(array(
'Bucket' => Configure::read('AWS.bucket'),
'Key' => $file_name,
'CAfile' => 'cacert.pem',
'ssl.CURLOPT_CAINFO' => 'cacert.pem',
'ssl.certificate_authority' => dirname(__FILE__) .DS. 'cacert.pem'));
} catch (S3Exception $e) {
echo "The file was not uploaded.\n";
}
}
public function list_objects(){
$s3 = $this->_s3;
$list = array();
foreach ($s3->getIterator('ListBuckets') as $bucket) {
foreach ($s3->getIterator('ListObjects', array('Bucket' => $bucket['Name'])) as $object) {
echo $bucket['Name'] . '/' . $object['Key'] . PHP_EOL;
array_push($list, $object['Key']);
}
}
return $list;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment