2

我设法让以下脚本处理较小的文件。但是当我尝试上传大约 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
        ));
    }
    }
4

1 回答 1

1

问题解决的问题是这条线

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

我把它注释掉了,它可以正常工作。没有大小限制。

于 2012-05-04T19:37:46.717 回答