0

Every image chosen from the user will be stored in Amazon S3 this is my code

  key: function ( file ) {
    var user = Meteor.users.findOne( this.userId );
    return user.emails[0].address + "/screenshots" + "/" + file.name;
  }

Based on my code, the image will be stored in the user's email directory then the screenshots folder then the file name. the problem is if the user wanted to store two images of the same name. The last picture will overwrite the first one.

i tried this solution

  key: function ( file ) {
    var today = new Date();
    var user = Meteor.users.findOne( this.userId );
    return user.emails[0].address + "/screenshots" + "/" + today + file.name;
  }

it didn't work because the current date have spaces so i can't load the image later in the client because of the spaces.

Any idea of a way i can use to give every image a unique name? with no spaces?

4

1 回答 1

1

您可以为每个生成一个随机的十六进制字符串:

key: function ( file ) {
    var unique = Random.hexString(16);;
    var user = Meteor.users.findOne( this.userId );
    return user.emails[0].address + "/screenshots" + "/" + unique + file.name;
}

Random 应该是您在创建 Meteor 项目时获得的默认软件包集的一部分。

于 2016-03-11T19:48:09.930 回答