如何使用Plupload HTML4上传S3?

时间:2012-07-05 16:10:48

标签: amazon-s3 plupload html4

我正在尝试使用Plupload的html4运行时来使用S3。不幸的是,example I found here必须是一个非常旧版本的plupload或没有正确记录,并且没有实时工作示例或源代码下载。

所以我将我的Pl​​upload / Flash运行时的工作版本带到S3并简单地放入html4运行时而不是flash并添加了'success_action_redirect': (我从plupload论坛了解到这是plupload需要知道什么时候文件上传完成)。我发现它确实有效,但有些脚本确实失败了。

这是我的源代码......

PHP:

<?php
$bucket = 'MyBucket';
$accessKeyId = 'MyKey';
$secret = 'MySecret';

// Policy Setup
if (!function_exists('hash_hmac')) : function hash_hmac($algo, $data, $key, $raw_output = false){
    $blocksize = 64;
    if (strlen($key) > $blocksize)
        $key = pack('H*', $algo($key));

    $key = str_pad($key, $blocksize, chr(0x00));
    $ipad = str_repeat(chr(0x36), $blocksize);
    $opad = str_repeat(chr(0x5c), $blocksize);
    $hmac = pack('H*', $algo(($key^$opad) . pack('H*', $algo(($key^$ipad) . $data))));

    return $raw_output ? $hmac : bin2hex($hmac);
}
endif;
// prepare policy
$policy = base64_encode(json_encode(array(
    'expiration' => date('Y-m-d\TH:i:s.000\Z', strtotime('+1 day')),  
    'conditions' => array(
        array('bucket' => $bucket),
        array('acl' => 'authenticated-read'),
        array('success_action_redirect' => 'http://mydomain.com/ThisCanBeAnyPage.php'),
        array('starts-with', '$key', ''),
        array('success_action_status' => '201'),
        array('starts-with', '$name', ''),  
        array('starts-with', '$Filename', ''), 
    )
)));
// Sign Policy
$signature = base64_encode(hash_hmac('sha1', $policy, $secret, true));
?>

使用Javascript:

<script type="text/javascript" src="js/plupload.full.js"></script>
<script type="text/javascript" src="js/plupload.html4.js"></script>
<script type="text/javascript">
// Custom example logic
$(function() {
    var uploader = new plupload.Uploader({
        preinit : {
            UploadFile: function(up, file) {
        // When file is loaded, directory on S3 is set here.
                up.settings.multipart_params.key = 'myFolder/'+file.name;
            }
        },
        runtimes : 'html4',
        browse_button : 'pickfiles',
        container : 'container',
        url : 'http://<?php echo $bucket; ?>.s3.amazonaws.com/',
        max_file_size : '300mb',
        multipart: true,
        multipart_params: {
            'key': '${filename}', // use filename as a key
            'success_action_redirect':'http://mydomain.com/ThisCanBeAnyPage.php',
            'Filename': '${filename}', // adding this to keep consistency across the runtimes
            'acl': 'authenticated-read',
            'success_action_status': '201',
            'AWSAccessKeyId' : '<?php echo $accessKeyId; ?>',       
            'policy': '<?php echo $policy; ?>',
            'signature': '<?php echo $signature; ?>'
        },
        file_data_name: 'file',
        multiple_queues: true,
        filters : [
            {title : "Image Files", extensions : "jpg,jpeg"},
        ],
        flash_swf_url : 'js/plupload.flash.swf',
        silverlight_xap_url : 'js/plupload.silverlight.xap'
    });

    uploader.bind('Init', function(up, params) {
        //$('#filelist').html("<div>Current runtime: " + params.runtime + "</div>");
    });

    $('#uploadfiles').click(function(e) {
        uploader.start();
        e.preventDefault();
    });

    uploader.bind('FilesAdded', function(up, files) {
        $.each(files, function(i, file) {
        // file.name seems to be the only thing that is returned so file.size is displayed as 'N/A'
            $('#filelist').append(
                '<div class="Section" id="'+file.id+'">'
                    +file.name+' ('+plupload.formatSize(file.size)+')&nbsp;&nbsp;<b>'
                    +'</b><div class="PlupLoadingBarWrap"><div class="PlupLoadingBar"></div></div>'
                +'</div>'
            );
        });
        up.refresh(); // Reposition Flash/Silverlight
    });

    uploader.bind('UploadProgress', function(up, file) {
        // file.name seems to be the only thing that is returned
        // This triggers when file is complete before 'FileUploaded' and not during upload process.
    });

    uploader.bind('Error', function(up, err) {
        // Haven't received an error to know if this works.
        $('#filelist').append("<div>Error: " + err.code +
            ", Message: " + err.message +
            (err.file ? ", File: " + err.file.name : "") +
            "</div>"
        );

        up.refresh(); // Reposition Flash/Silverlight
    });

    uploader.bind('FileUploaded', function(up, file) {
        // file.name seems to be the only thing that is returned
        // This does trigger once file is completely loaded
    });

    uploader.bind('UploadComplete', function(up, files){
        // Calls a function
    });

    uploader.init();
});
</script>

HTML:

<h3>Custom example</h3>
<div id="container">
    <div id="filelist">No runtime found.</div>
    <br />
    <a id="pickfiles" href="#">[Select files]</a>
    <a id="uploadfiles" href="#">[Upload files]</a>
</div>

总而言之,这个脚本确实有效但缺少file.size,并且根据我能够找到的内容没有进度指示。我想知道是否有一个plupload html4脚本,其中包含了这里缺少的东西。我有一个解决方法,我还没有测试。但我会保持更新。

1 个答案:

答案 0 :(得分:0)

简单的答案是否定的。

HTML4本身不支持任何这些功能。

但如果您使用提交功能,则可以执行此操作。基本上,您需要在客户端上设计自己的块,使用post提交块并控制块的数量以创建进度条。它在理论上很简单,但在实践中很难看。为什么可以使用闪光灯或银光注射?