ds000001 2012-11-06 11:28
浏览 68
已采纳

允许用户将MP3上传到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?

  • 写回答

4条回答 默认 最新

  • dongzhe3573 2012-11-22 10:37
    关注

    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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 怎么在stm32门禁成品上增加记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 解riccati方程组