2016-05-12 99 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。 –