Angularjs 如何设置唯一的aws S3文件名?

Angularjs 如何设置唯一的aws S3文件名?,angularjs,amazon-web-services,amazon-s3,mongoose,Angularjs,Amazon Web Services,Amazon S3,Mongoose,我通过前端将文件发送到s3存储桶,因为从我所读到的内容来看,这似乎更有效 但是,对于我的一些模式/集合,我将没有与文件/照片关联的id,因为它们是在上载的同时创建的: $scope.add = function(){ if($scope.file.photo){ $scope.distiller.photo = 'http://s3.amazonaws.com/whiskey-upload/distillers/' + ' needs to be assign

我通过前端将文件发送到s3存储桶,因为从我所读到的内容来看,这似乎更有效

但是,对于我的一些模式/集合,我将没有与文件/照片关联的id,因为它们是在上载的同时创建的:

  $scope.add = function(){

  if($scope.file.photo){
      $scope.distiller.photo = 'http://s3.amazonaws.com/whiskey-upload/distillers/' 
      + ' needs to be assigned to guid or collection id'
        Distillery.create($scope.distiller).then(function(res){
          console.log(res);
          $scope.distillers.push(res.data.distiller);
      var files = $scope.file;
      var filename = files.photo.$ngfName;
      var type = files.type;
      var folder = 'distillers/';

      var query = {
          files: files,
          folder: folder,
          filename: res.data.distiller._id,
          type: type
        };

        Uploads.awsUpload(query).then(function(){
          $scope.distiller = {};
          $scope.file = {};
        });
    });
  }
  else{
    Distillery.create($scope.distiller).then(function(res){
      toastr.success('distillery created without photo');
      $scope.distiller = {};
    });
  }
  };
上面的代码不会工作,除非我在蒸馏器对象创建之后以及文件上传到s3之后发送了关于aws.Upload承诺的更新

那似乎效率不高

我可以创建一个guid并将其分配给s3文件名,还可以在蒸馏器对象上保留该guid的引用。不过,这似乎有点骇人听闻

示例Guid创建者:

    function guid() {
  function s4() {
    return Math.floor((1 + Math.random()) * 0x10000)
      .toString(16)
      .substring(1);
  }
  return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
    s4() + '-' + s4() + s4() + s4();
}

实现我想要的最干净的方式是什么

好的GUID生成器是解决唯一id问题的非常标准的方法。根据算法的不同,名称冲突的可能性可能接近零。正如您所知,JavaScript没有本地版本,所以像您这样的版本是合理的。我认为它一点也不粗俗

以下是另一位作者:


您可能想研究使用FineUploader这样的包,它会接近于零吗?假设我有一个像instagram这样的应用程序,“为了简单起见,假设概率一致,那么到2014年,如果地球上每个人都拥有6亿个GUI,那么一个重复的概率大约为50%。”我不确定我是否理解这种概率,但它似乎很低。有关更多信息,请参阅en.wikipedia.org/wiki/Globally_unique_identifier。
function generateUUID() {
     var d = new Date().getTime();
     var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
         var r = (d + Math.random()*16)%16 | 0;
         d = Math.floor(d/16);
        return (c=='x' ? r : (r&0x3|0x8)).toString(16);
     });
     return uuid; };