It looks like you're new here. If you want to get involved, click one of these buttons!
POST ... Array
(
[Filename] => example.mov
[folder] => /video/manage/
[fileext] => *.mov;
[Upload] => Submit Query
)
<html>
<head>
<script type=\"text/javascript\" src=\"jquery-1.3.2.min.js\"></script>
<script type=\"text/javascript\" src=\"swfobject.js\"></script>
<script type=\"text/javascript\" src=\"jquery.uploadify.v2.1.0.js\"></script>
<link rel=\"stylesheet\" href=\"uploadify.css\" type=\"text/css\" media=\"screen\" />
</head>
<body>
<form>
<input id=\"fileInput\" name=\"fileInput\" type=\"file\" />
</form>
<script type=\"text/javascript\">// <![CDATA[
$(document).ready(function() {
$('#fileInput').uploadify({
'fileDataName' : 'file',
'uploader' : 'uploadify.swf',
'script' : 'http://UPLOADBUCKET/',
'cancelImg' : 'cancel.png',
'auto' : true,
'onError' : function(errorObj, q, f, err) { console.log(err); },
'scriptData' : {
AWSAccessKeyId: \"ACCESS_KEY\",
key: \"foo/${filename}\",
acl: \"public-read\",
policy: \"POLICY STRING\"
signature: \"SIGNATURE,
success_action_status: '200'
}
});
});
// ]]></script>
</body>
</html>
{ \"expiration\": \"2100-12-01T12:00:00.000Z\",
\"conditions\": [
{\"acl\": \"public-read\" },
{\"bucket\": \"UPLOADBUCKET\" },
{\"success_action_status\" : '200'},
[\"starts-with\", \"$filename\", \"\" ],
[\"starts-with\", \"$folder\", \"\" ],
[\"starts-with\", \"$key\", \"foo\"],
[\"content-length-range\", 1, 209715200]
]
}
terebentina said:Ha, nailed it!
There are several issues to overcome when using uploadify with s3:
1. as mentioned above uploadify sends 3 more params to the server so you need to add them in your policy
2. flashvars are urldecoded (once by javascript and once, I think, by flash). So you need to encodeURIComponent(encodeURIComponent()) for the parameters passed to scriptData. Especially the policy and signature. Otherwise you will get the "signature not matched" error from amazon.
3. on windows (flash 10) the 201 code is captured by the onComplete function. You receive the xml string passed by amazon in the 4th param (response). On mac though the 201 code is captured by the onError. You need to test for errorObj.type == 201 and treat that as an onComplete. Unfortunately you don't get the response in any param in onError so if you need to know the filename of the upload you will have to read it from fileObj.name.
I really recommend Wireshark for debugging the http traffic. It's been very helpful.
<script type='text/javascript'>
$(function() {
$('#upload-files').uploadify({
'fileDataName': 'file',
'uploader': 'uploadify.swf',
'script': 'BUCKET',
'method': 'post',
'auto': true,
'scriptAccess':'always',
'onError': function(e, q, f, err) { window.console.log(err); },
'scriptData': {
AWSAccessKeyId: 'AKIAJERVIXBJLFLRMBQA',
key: '${filename}',
acl: 'public-read',
policy: 'policy',
signature: 'signature',
}
})
});
policy_document = {
'expiration': '2015-01-01T00:00:00Z',
'conditions': [
{'bucket': settings.AWS_BUCKET_NAME},
[\"content-length-range\", 1, 209715200],
{'acl': 'public-read'},
{'success_action_status': 201},
[\"starts-with\", \"$Filename\", \"\" ],
[\"starts-with\", \"$folder\", \"\"],
{\"key\":\"${filename}\"},
[\"starts-with\", \"$Content-Type\", \"image/\"],
{\"Upload\": \"Submit Query\"},
]
}
{'AWSAccessKeyId':['ACCESSKEY'],
'Upload': ['Submit Query'],
'acl': ['public-read'],
'key': ['${filename}'],
'signature': ['SIGNATURE'],
'policy': ['POLICY='],
'folder': ['/foo/'],
'Filename': ['IMG_0200.JPG']}
$(\"#upload_btn\").uploadify({
'uploader' : 'static/flash/uploadify.swf',
'script' : 'http://bucket.s3.amazonaws.com/',
'scriptData' : {
\"AWSAccessKeyId\": encodeURIComponent(encodeURIComponent(\"your aws key\")),
\"key\": encodeURIComponent(encodeURIComponent(\"40/20091216-120535/${filename}\")),
\"acl\": \"private\",
\"policy\": encodeURIComponent(encodeURIComponent(\"your policy string\")),
\"signature\": encodeURIComponent(encodeURIComponent(\"signature string\")),
\"success_action_status\": \"201\",
\"folder\": encodeURIComponent(encodeURIComponent(\"\")),
\"fileext\": encodeURIComponent(encodeURIComponent(\"\")),
\"Filename\": encodeURIComponent(encodeURIComponent(\"\"))
},
'fileDataName' : 'file',
'scriptAccess' : 'always',
'fileExt' : '*.jpg;*.jpeg;*.png;*.gif',
'fileDesc' : 'Images',
'onError' : function(a, b, c, d) {
if (d.info == 201) {
var $queue = $('#queue').html('Processing, please wait...').show();
$.post(
'user/s3flphoto/f2507de7ea81f68b3eddcdfd876fd144',
{'fn': c.name},
function(data) {
$queue.hide();
}
);
} else {
page_message('Ughh, something wrong happened. Can you please try again?', 1);
}
},
'onComplete' : function(a, b, c, d, e) {
var $queue = $('#queue').html('Processing, please wait...').show();
$.post(
'user/s3flphoto/f2507de7ea81f68b3eddcdfd876fd144',
{'xml': d},
function(data) {
$queue.hide();
}
);
},
'folder' : '',
'queueID' : 'queue',
'buttonText' : 'Browse',
'auto' : true,
'method' : 'post',
'multi' : false
});
$post_params = Fileop::instance('s3')->getHttpUploadPostParams('bucket', $key, 'private', 3600, 5242880, 201, array(), array('folder'=>'', 'fileext'=>'', 'Filename'=>''));
.....................
'scriptData' : {
<?php
$output = '';
foreach ($post_params as $p => $v) {
$output .= '\"'.$p.'\": encodeURIComponent(encodeURIComponent(\"'.$v.'\")),'.\"\n\";
}
echo substr($output, 0, -2);
?>
},
'fileDataName' : 'file',
....................
Signature = Base64( HMAC-SHA1( UTF-8-Encoding-Of( YourSecretAccessKeyID, StringToSign ) ) );
StringToSign = HTTP-Verb + \"\n\" +
Content-MD5 + \"\n\" +
Content-Type + \"\n\" +
Date + \"\n\" +
CanonicalizedAmzHeaders +
CanonicalizedResource;
<?php
require_once 'S3.php';
$s3 = new S3(ACCESS_KEY, SECRET_KEY);
$key = $_GET['key'];
$json = array();
$json = $s3->getHttpUploadPostParams(BUCKET, $key, 'private', 3600, 5242880, 201, array(), array('folder'=>'', 'Filename'=>''));
$encoded = json_encode($json);
die($encoded);
?>
$.get('getS3Info.php', {'key': 'some/path/here/'},
function(data){
var scriptData = {
'AWSAccessKeyId': data.AWSAccessKeyId,
'key': data.key,
'acl': \"private\",
'policy': data.policy,
'signature': data.signature,
'success_action_status': \"201\",
'folder':'',
'Filename':''
};
$('#upload_link').uploadifySettings('scriptData',scriptData);
},
\"json\" );
$('#upload_link').uploadify({
'uploader': 'uploadify.swf',
'script': 'http://BUCKET.s3.amazonaws.com',
'multi': 'true',
'auto':'true',
'method':'post',
'scriptAccess':'always',
'onError':function(e, queueID, fileObj, errorObj){
if(errorObj.info == 201){
$('#upload_link'+queueID).remove();
} else {
alert(errorObj.type+' error:'+errorObj.info+'. Sorry! (try again later)');
}
},
'folder':'',
'fileDataName':'file'
});
I've been able to make it work by including 'folder', 'fileext' and 'Filename' in the policy
...but now the problem is that sometimes the upload works, sometimes it returns SignatureDoesNotMatch.
$post_params = Fileop::instance('s3')->getHttpUploadPostParams('bucket', $key, 'private', 3600, 5242880, 201, array(), array('folder'=>'', 'fileext'=>'', 'Filename'=>''));$post_params = Fileop::instance('s3')->getHttpUploadPostParams('bucket', $key, 'private', (60*60*24 * 1), 5242880, 201, array(), array('folder'=>'', 'fileext'=>'', 'Filename'=>''));public function gets3info()
{
header('Content-type: application/json');
$s3 = new S3('your-access-key', 'your-secret-key');
$key = $_GET['key'];
$bucket = \"yourbucket\";
$json = $s3->getHttpUploadPostParams($bucket, $key, 'private', (60*60*24 * 1), (1024 * 1024 * 2000), 201, array(), array('folder'=>'', 'Filename'=>''));
# Make sure we don't have a signature with a '+' because Actionscript 3 encodes them as spaces instead of %2B
if (strpos($json->signature, '+') !== FALSE)
{
$this->gets3info();
}
else
{
$json->bucket = $bucket;
$encoded = json_encode($json);
die($encoded);
}
}
if (param.scriptData != '') variables.decode(unescape(param.scriptData));
if (param.scriptData != '') variables.decode(unescape(param.scriptData).replace('+', '%2B'));"AWSAccessKeyId": encodeURIComponent(encodeURIComponent("Amazon Key")), "key": encodeURIComponent(encodeURIComponent("${filename}")), "acl": encodeURIComponent(encodeURIComponent("private")), "policy": encodeURIComponent(encodeURIComponent("eyJleHBpcmF0aW9uIjoiMjAxMS0wMS0yOFQwMjowMTowNVoiLCJjb25kaXRpb25zIjpbeyJidWNrZXQiOiJpc2RtdXNpYyJ9LHsiYWNsIjoicHJpdmF0ZSJ9LHsic3VjY2Vzc19hY3Rpb25fc3RhdHVzIjoiMjAxIn0sWyJzdGFydHMtd2l0aCIsIiRrZXkiLCIiXSxbInN0YXJ0cy13aXRoIiwiJGZvbGRlciIsIiJdLFsic3RhcnRzLXdpdGgiLCIkRmlsZW5hbWUiLCIiXSxbImNvbnRlbnQtbGVuZ3RoLXJhbmdlIiwwLDUyNDI4ODAwXV19")), "signature": encodeURIComponent(encodeURIComponent("/SY2fMBopByN90qU27EUFClT52c=")), "success_action_status": encodeURIComponent(encodeURIComponent("201")), "folder": encodeURIComponent(encodeURIComponent("")), "Filename": encodeURIComponent(encodeURIComponent(""))<html>
<head>
<script type="text/javascript" src="up/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="up/swfobject.js"></script>
<script type="text/javascript" src="up/jquery.uploadify.v2.1.4.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#uploadify_div').uploadify({
'uploader': 'up/uploadify.swf',
'script' : '<a href="http://bucket.s3.amazonaws.com" target="_blank" rel="nofollow">http://bucket.s3.amazonaws.com</a>',
'scriptData' : {
<?php
$output = '';
foreach ($post_params as $p => $v) {
$output .= '"'.$p.'": encodeURIComponent(encodeURIComponent("'.$v.'")),'."\n";
}
echo substr($output, 0, -2);
?>
},
'fileDataName' : 'file',
'scriptAccess' : 'always',
'onError' : function(a, b, c, d) {
if (d.info == 201) {
var $queue = $('#queue').html('Processing, please wait...').show();
$.post(
'user/s3flphoto/f2507de7ea81f68b3eddcdfd876fd144',
{'fn': c.name},
function(data) {
$queue.hide();
}
);
} else {
page_message('Ughh, something wrong happened. Can you please try again?', 1);
}
},
'onComplete' : function(a, b, c, d, e) {
var $queue = $('#queue').html('Processing, please wait...').show();
$.post(
'user/s3flphoto/f2507de7ea81f68b3eddcdfd876fd144',
{'xml': d},
function(data) {
$queue.hide();
}
);
},
'folder' : '',
'auto' : true,
'multi' : false
});
});
</script>
</head>
<body>
<div id="uploadify_div"></div>
</body>
</html>Here is my uploadify code what am i using on the rails + Amazon S3 env...
$(document).ready(function() {
$('#custom_file_upload').click(function(event){
event.preventDefault();
});
$('#custom_file_upload').uploadify({
buttonText: 'Select filess',
scriptAccess: 'always',
fileExt: '*.jpg;*.jpeg;*.png;*.gif',
fileDesc: 'Images',
auto: true,
method: 'post',
multi: true,
uploader: '/javascripts/uploadify/uploadify.swf',
cancelImg: '/javascripts/uploadify/cancel.png',
script : "<a href='https://bucket.s3.amazonaws.com' target='_blank' rel='nofollow'>https://bucket.s3.amazonaws.com</a>",
scriptData: {
'AWSAccessKeyId': 'awsaccesskey',
'key': 'myserectkey',
'acl': 'private',
'policy': 'mypolicy',
'signature': 'ma76ECHhpy1GAeIdTZrcCr8YFvg=',
'success_action_status': '201',
'folder': 'user_files/prince/11-11-2011_14:45:16/${filename}',
'Filename': '11-11-2011_14:45:16'
},
onComplete: function(a, b, c, d, e) {
var $queue = $('#queue').html('Processing, please wait...').show();
$.post( 'user/s3flphoto/f2507de7ea81f68b3eddcdfd876fd144',
{'xml': d},
function(data) {
$queue.hide();
}
);
}
});
$('#upload_submit').click(function(event){
event.preventDefault();
$('#upload_photo').uploadifyUpload();
});
});
</script>
<h3>
<div class='text'>Upload file</div>
<div class='clear'></div>
</h3>
<div class='uploadForm'>
<div id="status-message">Select some files to upload:</div>
<input id="custom_file_upload" type="file" name="Filedata" />
</div>
Actually, whenever i add a new file for uploading. Always it struck at middle of process sometimes 8% and sometimes 31% .. And there is nothing come into the bucket folder structure as well..
Please help me out for fixing this bug...
Many thanks in advance