简体   繁体   中英

How to assign default directory for every image stored in Amazon S3?

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?

You can generate a random hex string for each one:

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 should be part of the default set of packages you get when you create a Meteor project.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM