Integrate Amazon S3 with Zend Framework
In this web development tutorial I will give you a few neccesary steps for uploading large files to an Amazon account using Zend Framework 1.
Note: If you want to use Amazon S3 for storing large filses, first you have to create an Amazon S3 account here. Please take note that the Amazon S3 is not a free service.
Step 1
Create an Amazon Access Keys
After following all the steps here, you will get the access keys, that have two parts and look like that:
- Access key ID example: AKIAIOSFODNN7EXAMPLE
- Secret access key example: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
Step 2
Create an Amazon Bucket
Follow the steps here to creat a Bucket and you will get something like that:
Step 3
Connect to Amazon S3 using Zend_Service_Amazon_S3 class
We will store our Access key ID, Secret access key and Bucket in some variables like bellow:
1 2 3 |
$amazonKey = 'AKIAIOSFODNN7EXAMPLE'; $amazonSecret = 'wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY '; $amazonBucket = 'businessbucketlogfiles'; |
After doing that, we instantiate Zend_Service_Amazon_S3 with keys:
1 2 |
require_once 'Zend/Service/Amazon/S3.php'; $s3 = new Zend_Service_Amazon_S3($amazonKey, $amazonSecret); |
Step 4
Creating objects
For this purpose we can use one of the following methods: putObject(), putFile() or putFileStream(). In our case we will use putFile().
1 2 3 4 5 6 |
require_once 'Zend/Service/Amazon/S3.php'; $s3 = new Zend_Service_Amazon_S3($amazonKey, $amazonSecret); $s3->putFile(myfilename, $amazonbucket."/".$myuploadedfile, array(Zend_Service_Amazon_S3::S3_ACL_HEADER => Zend_Service_Amazon_S3::S3_ACL_PUBLIC_READ)); |
When we create new objects we can set attributes for each file. An exemple is the availability for the users. In our case users can only read files.
Modifying an existing object
For modify an object we simply use the method call for creating the object and the object will be replaced.
Deleting an existant object
For remove an object fro Amazon S3 we call removeObject() method.
1 |
$s3->removeObject($amazonbucket. '/'.$myuploadedfile); |
Get information about an object
- isObjectAvailable() method: check if the object exists
1 |
$s3->isObjectAvailable($amazonbucket. '/'.$myuploadedfile); |
- getInfo()
- Retrieves information about a given object
- outputs:
- file type
- file size
- mtime
- etag
1 |
$s3->getInfo($amazonbucket. '/'.$myuploadedfile); |
Step 5
How can outside users access files you store in Amazon S3 buckets?
Above are listed the posibilities:
- http://[bucketName].s3.amazonaws.com/[objectName]
- http://s3.amazonaws.com/[bucketName]/[objectName]
Summary
This was a basic tutorial how to work with Amazon S3 service using Zend Framework v1. For any other question regarding this subject, don’t hesitate to post your question below.
Posted by Design19 on Thursday, July 2, 2015