Wednesday, 14 May 2014

PHP Upload File In Amazon S3 Bucket - Code Snippets

PHP Upload File In Amazon S3 Bucket - Code Snippets

Get List of buckets in Amazon S3 bucket and upload a file in Amazon S3 bucket with Zend Framework.

Uploading files in zend framework is very simple because zend framework provides the API which is very simple to use.

Please use following code to upload photos in zend framework.
$my_aws_key = 'AWS_KEY';
$my_aws_secret_key = 'AWS_SECRET_KEY';
$s3 = new Zend_Service_Amazon_S3($my_aws_key, $my_aws_secret_key);

/** Get List of buckets * */
$list = $s3->getBuckets();
foreach ($list as $bucket) {
    echo "Bucket: $bucket\n";
}
/** Get List of buckets * */


/** get Bucket Files **/
$files = $s3->getObjectsByBucket("BUCKET_NAME");
foreach ($files as $file) {
    echo "File: $file\n";
}
/** get Bucket Files **/


$imageName = 'http://static.zend.com/img/yellowpages/ZFCE-logo-XS.jpg';
try {
    $s3->putFile($imageName, "BUCKET_NAME/ZFCE-logo-XS.jpg", array(
        Zend_Service_Amazon_S3::S3_ACL_HEADER => Zend_Service_Amazon_S3::S3_ACL_PUBLIC_READ,
        'x-amz-storage-class' => 'REDUCED_REDUNDANCY'
            )
    );
die('uploaded successfully');
} catch (Exception $e) {
    pr($e->getMessage());
    die;
}