2
小さいファイルを扱うために、次のスクリプトを手に入れました。しかし、10MB以上のファイルをアップロードしようとすると、それは完了していますが、ファイルはS3バケットに表示されません。大きなファイルをAmazon 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
));
}
}