将大文件上传到 Amazon S3

问题描述 投票:0回答:1

我已经设法让以下脚本能够处理较小的文件。但是,当我尝试上传大约 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
        ));
    }
    }
php amazon-s3 uploadify
1个回答
1
投票

问题已解决,问题出在这条线上

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

我把它注释掉了,它按预期工作了。大小没有限制。

© www.soinside.com 2019 - 2024. All rights reserved.