2016-05-12 13 views
1

ファイルをフロントエンドからs3バケットに送信しています。これは、私が読んだものより効率的です。ユニークなaws 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の作成者:

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にはネイティブ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を所有する場合、1つの複製の確率は約50%になります。私はその確率を理解しているか分からないが、それは低いようだ。詳細は、en.wikipedia.org/wiki/Globally_unique_identifierを参照してください。 –

関連する問題