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?

Answers


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.


Need Your Help

32bit dll used by VB6 program doesn't work in 64bit Win 7

windows dll vb6

1) a 32bit dll is placed in C:\Windows\SysWOW64

Get javascript changed Text as String

javascript string get find indexof

I was wondering if it's possible to get text which was changed by JavaScript as a String.