HTTP file upload to Amazon S3

I have been experimenting with the two new components of the Zend Framework recently, Zend_File_Transfer and Zend_Service_Amazon_S3, in particular I have been experimenting with HTTP file uploading straight to an Amazon S3 bucket. The great thing about both the components is that they work brilliantly together and I had a working script in minutes.

Here is my code:

/**
 * @see Zend_Service_Amazon_S3
 */
require_once 'Zend/Service/Amazon/S3.php';
Zend_Service_Amazon_S3::setKeys('AccessKey', 'SecretKey');

if (!stream_wrapper_register('s3', 'Zend_Service_Amazon_S3')) {
    throw new Zend_Service_Exception('Could not register S3 stream');
}

/**
 * @see Zend_Form
 */
require_once 'Zend/Form.php';
$form = new Zend_Form();
$form->setMethod('post');
$form->setAttrib('enctype', 'multipart/form-data');

$form->addElement('file', 'file', array(
    'label' => 'File to upload',
    'required' => true,
    'destination' => 's3://noginn',
));

$form->addElement('submit', 'upload', array(
    'label' => 'Upload',
    'ignore' => true,
));

if ($this->_request->isPost()) {
    $data = $this->_request->getPost();
    if ($form->isValid($data)) {
        $fileTransfer = $form->file->getTransferAdapter();
        $fileTransfer->receive();
    }
}

$this->view->form = $form;

As you can see the use of Zend_File_Transfer is made even more convenient by the introduction of Zend_Form_Element_File, which provides a view helper and utilises the file transfer adapter internally so file uploads can be validated in the same manor as any other input.

The S3 component is registered as the stream wrapper ’s3://’, allowing it to be used with all standard file system functions such as fopen(), fread(), and more importantly move_uploaded_file(). This makes integration with the file transfer component seamless, as files can be saved straight to an S3 bucket by simply setting the file transfer destination as ’s3://bucket_name’.

The Zend_Service_Amazon_S3 proposal is currently in the laboratory and is still being finalised. It can be downloaded from the subversion repository.

Please note that the code example provided is the simplest usage of the components and should not be used without first improving security.

1 Comment

1. Naved posted on 04/10/2008 at 12:31 PM

Hi,

I tried to use your code, but I dont know why I am getting this error

Fatal error: Uncaught exception ‘Zend_Uri_Exception’ with message ‘Invalid URI supplied’ in C:\apache2triad\htdocs\amazon-s3\library\Zend\Uri\Http.php:136 Stack trace: #0 C:\apache2triad\htdocs\amazon-s3\library\Zend\Uri.php(123): Zend_Uri_Http->__construct(’http’, ‘//s3.amazonaws….’) #1 C:\apache2triad\htdocs\amazon-s3\library\Zend\Http\Client.php(251): Zend_Uri::factory(’http://s3.amazo…’) #2 C:\apache2triad\htdocs\amazon-s3\library\Zend\Http\Client.php(237): Zend_Http_Client->setUri(’http://s3.amazo…’) #3 C:\apache2triad\htdocs\amazon-s3\library\Zend\Service\Amazon\S3.php(615): Zend_Http_Client->__construct(’http://s3.amazo…’) #4 C:\apache2triad\htdocs\amazon-s3\library\Zend\Service\Amazon\S3.php(528): Zend_Service_Amazon_S3->_makeRequest(’HEAD’, ’s3://******…’) #5 C:\apache2triad\htdocs\amazon-s3\library\Zend\File\Transfer\Adapter\Http.php(98): Zend_Service_Amazon_S3->url_stat(’s3://******…’, 2) #6 C:\apache2triad\htdocs\amazon-s3\library\Zend\Form\Element\File.php(275): Zend_File_Transfer_Adapter_ in C:\apache2triad\htdocs\amazon-s3\library\Zend\Uri\Http.php on line 136

Have your say

(Never published)
(Optional)

About the author

Tom Graham is a web developer living in Leicester, UK. This blog is his vent for all things web design and development. Read more about Tom.

Categories

Pages

Attending

phpnw08 PHP Conference 22/11/2008