Martin Bean
Martin Bean

Reputation: 39419

Allowing a user to upload an MP3 to Amazon S3

I’m working on a website where users can upload podcasts. Podcasts are to be MP3 files, and stored in an Amazon S3 bucket.

What is the normal flow for this? I’ve Googled, but any articles relating to file uploads tend to be using the Amazon client libraries, and ideally I don’t want to use PHP (I’m using the LAMP stack) for uploading an MP3 file due to timeouts, file size limits etc.

Is there a way around this?

Upvotes: 5

Views: 2713

Answers (4)

Martin Bean
Martin Bean

Reputation: 39419

After lots of Googling, and back-and-forth on both the GitHub repository and Amazon documentation for the new PHP SDK, I’ve got a solution using Amazon’s new PHP SDK to generate the form fields, and Uploadify to actually upload the file directly to Amazon, bypassing my server. The code looks a little like this:

<?php
$bucket = (string) $container['config']->images->amazon->bucket;
$options = array(
    'acl' => CannedAcl::PUBLIC_READ,
    'Content-Type' => 'audio/mpeg',
    'key' => 'audio/a-test-podcast.mp3',
    'success_action_redirect' => (string) $container['config']->main->base_url . 'upload/success/',
    'success_action_status' => 201,
    'filename' => '^'
);
$postObject = new PostObject($container['amazon_s3'], $bucket, $options);
$postObject->prepareData();

$formAttributes = $postObject->getFormAttributes();
$formInputs = $postObject->getFormInputs();

$uploadPath = $formAttributes['action'];
?>
<script>
    (function($) {
        $('#podcast').uploadify({
            'buttonClass': 'button',
            'buttonText': 'Upload',
            'formData': <?php echo json_encode($formInputs); ?>,
            'fileObjName': 'file',
            'fileTypeExts': '*.mp3',
            'height': 36,
            'multi': false,
            'onUploadError': function(file, errorCode, errorMsg, errorString) {
                console.log('onUploadError', file, errorCode, errorMsg, errorString);
            },
            'onUploadSuccess': function(file, data, response) {
                console.log('onUploadSuccess', file, data, response);
            },
            'swf': '/assets/cms/swf/uploadify.swf',
            'uploader': '<?php echo $uploadPath; ?>',
            'width': 120
        });
    })(jQuery);
</script>

Upvotes: 1

Michel Feldheim
Michel Feldheim

Reputation: 18250

Requests to S3 must be signed with a valid secret key and always include authorization. You can do this at client side with JavaScript by theory but this would involve Ajax calls to sign your headers if you don't want to expose your secret key to the client side. Another problem will be browser compatibility. You'll have to use file slicing and calculate checksums on bigger files which is available in all modern browsers.

The simpler method would be the server-side solution with PHP or any other server-side scripting language

Upvotes: 0

Vivek Muthal
Vivek Muthal

Reputation: 1015

You dont have to worry about the file size though you use php you can configure the upload file size limits and execution memory limit in php.ini file and php libraries for s3 will reduce your work. You can start from this guide.

Upvotes: 0

user1480400
user1480400

Reputation:

Amazon S3 supports direct uploads. This might be an option here. For PHP implementation, check out this post.

Upvotes: 4

Related Questions