私はここで見つけるスマートパッケージとしてNPMモジュールを包んだ:それでhttps://atmosphere.meteor.com/package/s3policies
をあなたは書き込みポリシーを返し、そのポリシーを使用すると、AJAX呼び出しを使用してS3にアップロードすることができ流星メソッドを作ることができます。
例:
Meteor.call('s3Upload', name, function (error, policy) {
if(error)
onFinished({error: error});
var formData = new FormData();
formData.append("AWSAccessKeyId", policy.s3Key);
formData.append("policy", policy.s3PolicyBase64);
formData.append("signature", policy.s3Signature);
formData.append("key", policy.key);
formData.append("Content-Type", policy.mimeType);
formData.append("acl", "private");
formData.append("file", file);
$.ajax({
url: 'https://s3.amazonaws.com/' + policy.bucket + '/',
type: 'POST',
xhr: function() { // custom xhr
var myXhr = $.ajaxSettings.xhr();
if(myXhr.upload){ // check if upload property exists
myXhr.upload.addEventListener('progress',
function (e){
if(e.lengthComputable)
onProgressUpdate(e.loaded/e.total * 100);
}, false); // for handling the progress of the upload
}
return myXhr;
},
success: function() {
// file finished uploading
},
error: function() { onFinished({error: arguments[1]}); },
processData: false,
contentType: false,
// Form data
data: formData,
cache: false,
xhrFields: { withCredentials: true },
dataType: 'xml'
});
});
EDIT:
ラインで "ファイル" 変数:formData.append("file", file);
はラインから次のようになります。var file = document.getElementById('fileUpload').files[0];
サーバー側のコードは次のようになります。
Meteor.methods({
s3Upload: function (name) {
var myS3 = new s3Policies('my key', 'my secret key');
var location = Meteor.userId() + '/' + moment().format('MMM DD YYYY').replace(/\s+/g, '_') + '/' + name;
if(Meteor.userId()) {
var bucket = 'my bucket';
var policy = myS3.writePolicy(location, bucket, 10, 4096);
policy.key = location;
policy.bucket = bucket;
policy.mimeType = mime.lookup(name);
return policy;
}
}
});
ファイルオブジェクトを流星法に渡すことができましたか?私は未定義になっています。 – Ankita