javascriptmeteoramazon-s3meteor-slingshot

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?


Solution

  • 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.