如何将额外的数据从客户端传递到弹弓s3storage?
问题描述:
我试图从FlowRouter.getParam('id');
通过用户ID到服务器上的文件上传到亚马逊。这是一个管理员帐户,所以我使用FlowRouter.getParam('id');
访问正确的用户的个人信息。问题是我没有正确地传递id,所以它只是错误并停止工作。如何将额外的数据从客户端传递到弹弓s3storage?
如何正确地传递了ID结束了吗?
路径uploadFile.js
let _uploadFileToAmazon = (file) => {
var id = FlowRouter.getParam('id');
const uploader = new Slingshot.Upload("uploadProfileImgAdmin", id);
uploader.send((file), (error, url) => {
if (error) {
Bert.alert(error.message, "warning");
_setPlaceholderText();
} else {
_addUrlToDatabase(url);
}
});
};
路径server/uploadFile.js
Slingshot.createDirective("uploadProfileImgAdmin", Slingshot.S3Storage, {
bucket: "bhr-app",
region: "ap-southeast-2",
acl: "public-read",
authorize: function (id) {
console.log("user id: ", id);
return Files.findOne({ "userId": id });
},
key: function (file) {
var user = Meteor.users.findOne(_id: id);
return "profile-images" + "/" + user.emails[0].address + "/" + file.name;
}
});
答
首先,为了获得当前用户的ID,你应该在authorize
方法在服务器上使用this.userId
和不只需信任客户端传递的数据(以确保用户实际上是管理员并验证参数)。
的meta-context添加到上传应该是一个对象(您正在传递一个字符串),它可以作为你的指导方法的第二个参数。
const uploader = new Slingshot.Upload("uploadProfileImgAdmin", {id});
和服务器上,你的指令的方法得到了file
和meta
你通过:
Slingshot.createDirective("uploadProfileImgAdmin", Slingshot.S3Storage, {
bucket: "bhr-app",
region: "ap-southeast-2",
acl: "public-read",
authorize: function (file, meta) {
console.log("user id: ", meta.id);
// validate meta, make sure that the user is an admin and
// return a Boolean or throw an error
},
key: function (file, meta) {
var user = Meteor.users.findOne(meta.id);
return "profile-images" + "/" + user.emails[0].address + "/" + file.name;
}
});
感谢。只是为了澄清:我知道你可以使用this.userId来访问服务器上的当前用户ID。在这种情况下,管理员正在编辑另一个帐户。所以我使用'FlowRouter.getParam('id');'将该id传递给服务器。如果有更好的方法我很想知道。 您的解决方案肯定是工作,但是,它现在((文件),(错误,URL)=>错误的''uploader.send它说用户没有授权。我试着加上'上传。发送((文件,{ID})(错误,URL)=>'这改变了错误,这不是一个文件。思考? – bp123
你编辑的授权方法来验证输入和管理员的身份? – MasterAM
这是问题。谢谢堆! – bp123