2016-05-12 98 views
1

我正在通过前端将我的文件发送到s3存储桶,因为从我读过的内容看,这似乎更有效率。如何设置一个独特的aws S3文件名?

但是,对于我的一些模式/收藏品,我不会有相应的文件/照片相关的标识 - 因为他们正在创建的同时上传:

$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文件名,并且还可以在该DRS对象上保留一个引用。不过,这看起来很诡异。

例子的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(); 
} 

什么是实现我想要的最彻底的方法?

+0

你可能要考虑使用像fineuploader – dkarchmer

回答

2

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

这里是另一个由@briguy37

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; }; 
+0

包怎么会接近于零呢?让我们说我有一个像Instagram的应用程序? – NoobSter

+0

“为简单起见,假设统一概率,如果2014年全球每个人拥有6亿个GUID,则一次重复的概率将约为50%。”不知道我明白这个可能性,但似乎很低。有关更多信息,请参见en.wikipedia.org/wiki/Globally_unique_identifier。 –