首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将额外的数据从客户端传递到slingshot s3storage?

如何将额外的数据从客户端传递到slingshot s3storage?
EN

Stack Overflow用户
提问于 2016-08-15 10:54:19
回答 1查看 121关注 0票数 0

我正在尝试将用户id从FlowRouter.getParam('id');传递到服务器,以便将文件上传到亚马逊。这是一个管理员帐户,所以我使用FlowRouter.getParam('id');来访问正确的用户配置文件信息。问题是我没有正确地传递id,所以它只会出错并停止工作。

如何正确传递id?

路径uploadFile.js

代码语言:javascript
复制
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

代码语言:javascript
复制
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;
  }
});
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-08-15 13:00:26

首先,为了获得当前用户的id,您应该在服务器上的authorize方法中使用this.userId,而不是简单地信任客户机传递的数据(以确保用户实际上是管理员并验证参数)。

添加到upload中的meta-context应该是一个对象(您正在传递一个字符串),并且它可以用作指令方法的第二个参数。

代码语言:javascript
复制
const uploader = new Slingshot.Upload("uploadProfileImgAdmin", {id});

在服务器端,您的指令方法获取您传递的filemeta

代码语言:javascript
复制
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;
  }
});
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38948707

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档