Created
November 6, 2013 13:48
-
-
Save ms2sato/7336318 to your computer and use it in GitHub Desktop.
S3へ直接ファイルをアップロードする。Node.js版 ref: http://qiita.com/ms2sato/items/f73a02f2ac14361247c3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var moment = require('moment'); | |
var crypto = require('crypto'); | |
function createS3bucket(params) { | |
function base64_encode(val) { | |
var b = new Buffer(val); | |
return b.toString('base64'); | |
} | |
var accesskey = '[accesskey]' // TODO | |
var secret = '[secret]'; // TODO | |
var bucket = '[bucket]'; // TODO | |
var key = '[path/to/file]'; // TODO | |
var aminlater = moment(new Date().getTime() + 1000 * 60); //a minit later | |
var expiration = aminlater.utc().format('YYYY-MM-DDTHH:mm:ss[Z]'); | |
var acl = 'public-read'; // set acl (private | public-read | public-read-write | authenticated-read | bucket-owner-read | bucket-owner-full-control) | |
var status = '200'; // return this status code when upload success. | |
var ctype = params['ctype']; | |
var clength = params['clength']; | |
var policy = { | |
"expiration": expiration, | |
"conditions": [ | |
{"bucket": bucket}, | |
{"key": key}, | |
{"acl": acl}, | |
{"success_action_status": status}, | |
{"Content-Type": ctype}, | |
["content-length-range", clength, clength] | |
] | |
}; | |
//console.log('policy', policy); | |
var encodedPolicy = base64_encode(JSON.stringify(policy)); | |
var sh = crypto.createHmac('sha1', secret); | |
sh.update(encodedPolicy); | |
var signature = sh.digest('base64'); | |
var ret = { | |
'url': 'http://' + bucket + '.s3.amazonaws.com/', | |
'form': { | |
'AWSAccessKeyId': accesskey, | |
'signature': signature, | |
'policy': encodedPolicy, | |
'key': key, | |
'acl': acl, | |
'success_action_status': status, | |
'Content-Type': ctype | |
} | |
}; | |
//sconsole.log('ret', ret); | |
return ret; | |
} | |
exports.upload = function (req, res) { | |
var ret = createS3bucket(req.query); | |
res.json(ret); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> | |
<script src="//code.jquery.com/jquery-1.10.1.min.js"></script> | |
<script> | |
$().ready(function () { | |
$('#s3form input[type="file"]').on('change', function (e) { | |
// ファイルの情報をチェックしてパラメータ生成.必要であれば許可ファイル以外をはじく. | |
var file = e.target.files[0]; | |
var data = {ctype: file.type, clength: file.size}; | |
// サーバからpolicyとsignatureを取得. | |
var $form = $('#s3form'); | |
$.ajax({ | |
url: $form.attr('action'), | |
type: $form.attr('method'), | |
dataType: 'json', | |
data: data | |
}).done(function (data) { | |
// サーバが返した情報をそのまま使ってFormDataを作る. | |
var name, fd = new FormData(); | |
for (name in data.form) if (data.form.hasOwnProperty(name)) { | |
fd.append(name, data.form[name]); | |
} | |
fd.append('file', file); // ファイル添付. | |
// 送信 | |
var xhr = new XMLHttpRequest(); | |
xhr.open('POST', data.url, true); | |
xhr.onreadystatechange = function (aEvt) { | |
if (xhr.readyState == 4) { | |
if (xhr.status == 200) | |
console.log(xhr.responseText); | |
else{ | |
console.log('error'); | |
console.log(xhr.responseText); | |
} | |
} | |
}; | |
xhr.send(fd); | |
}) | |
}); | |
}); | |
</script> | |
</head> | |
<body> | |
<form id="s3form" action="upload" method="get" enctype="multipart/form-data"> | |
<input type="file" name="file"/> | |
</form> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment