将大文件上载到Amazon S3

时间:2012-05-04 18:42:01

标签: amazon-s3 uploadify

我已设法使用以下脚本来处理较小的文件。但是当我尝试上传大约10MB或更多的文件时,它表示它已经完成但文件没有显示在我的S3存储桶中。

为什么它会上传较小的文件而不是10MB或更大的文件?

<?php
//System path for our website folder
define('DOCROOT', realpath(dirname(__FILE__)).DIRECTORY_SEPARATOR);
//URL for our website
define('WEBROOT', htmlentities(
    substr($_SERVER['REQUEST_URI'], 0, strcspn($_SERVER['REQUEST_URI'], "\n\r")),
    ENT_QUOTES
));

//Which bucket are we placing our files into
$bucket = 'bucket.mysite.com';
// This will place uploads into the '20100920-234138' folder in the $bucket bucket
$folder = date('Ymd-His').'/'; //Include trailing /

//Include required S3 functions
require_once DOCROOT."includes/s3.php";

//Generate policy and signature
list($policy, $signature) = S3::get_policy_and_signature(array(
    'bucket'        => $bucket,
    'folder'        => $folder,
    ));
?>


<script type="text/javascript">
    $(document).ready(function() {
        $("#file_upload").uploadify({
            'uploader'      : '<?= WEBROOT ?>files/uploadify/uploadify.swf',
            'buttonText'        : 'Browse',
            'cancelImg'     : '<?= WEBROOT ?>files/uploadify/cancel.png',
            'script'        : 'http://s3.amazonaws.com/<?= $bucket ?>',
            'scriptAccess'      : 'always',
            'method'        : 'post',
            'scriptData'        : {
                "AWSAccessKeyId"        : "<?= S3::$AWS_ACCESS_KEY ?>",
                "key"               : "${filename}",
                "acl"               : "authenticated-read",
                "policy"            : "<?= $policy ?>",
                "signature"         : "<?= $signature ?>",
                "success_action_status"     : "201",
                "key"               : encodeURIComponent(encodeURIComponent("<?= $folder ?>${filename}")),
                "fileext"           : encodeURIComponent(encodeURIComponent("")),
                "Filename"          : encodeURIComponent(encodeURIComponent(""))
            },
            'fileExt'       : '*.*',
            'fileDataName'      : 'file',
            'simUploadLimit'    : 2,
            'multi'         : true,
            'auto'          : true,
            'onError'       : function(errorObj, q, f, err) { console.log(err); },
            'onComplete'        : function(event, ID, file, response, data) { console.log(file); }
        });
    });
</script>

 <?php
    class S3 {
        public static $AWS_ACCESS_KEY       = '< Your access key >';
        public static $AWS_SECRET_ACCESS_KEY    = '< Your secrete key >';


    /*
     * Purpose:
     *      Actionscript encodes '+' characters in the signature incorrectly - it makes
     *      them a space instead of %2B the way PHP does. This causes uploadify to error
     *      out on upload. This function recursively generates a new policy and signature
     *      until a signature without a + character is created.
     * Accepts: array $data
     * Returns: policy and signature
     */
    public static function get_policy_and_signature( array $data )
    {
        $policy = self::get_policy_doc( $data );
        $signature = self::get_signature( $policy );

        if ( strpos($signature, '+') !== FALSE )
        {
            $data['timestamp'] = intval(@$data['timestamp']) + 1;
            return self::get_policy_and_signature( $data );
        }

        return array($policy, $signature);
    }

    public static function get_policy_doc(array $data)
    {
        return base64_encode(
            '{'.
                '"expiration": "'.gmdate('Y-m-d\TH:i:s\Z', time()+60*60*24+intval(@$data['timestamp'])).'",'.
                '"conditions": '.
                '['.
                    '{"bucket": "'.$data['bucket'].'"},'.
                    '["starts-with", "$key", ""],'.
                    '{"acl": "authenticated-read"},'.
                    //'{"success_action_redirect": "'.$SWFSuccess_Redirect.'"},'.
                    '{"success_action_status": "201"},'.
                    '["starts-with","$key","'.str_replace('/', '\/', $data['folder'] ).'"],'.
                    '["starts-with","$Filename",""],'.
                    '["starts-with","$folder",""],'.
                    '["starts-with","$fileext",""],'.
                    '["content-length-range",0,5242880]'.
                ']'.
            '}'
        );
    }

    public static function get_signature( $policy_doc ) {
        return base64_encode(hash_hmac(
            'sha1', $policy_doc, self::$AWS_SECRET_ACCESS_KEY, true
        ));
    }
    }

1 个答案:

答案 0 :(得分:1)

问题解决的问题是这一行

'["content-length-range",0,5242880]'

我评论了它并且它应该工作。没有限制。