首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何设置唯一的aws S3文件名?

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

Stack Overflow用户
提问于 2016-05-12 21:51:39
回答 1查看 6.7K关注 0票数 4

我正在通过前端将我的文件发送到一个s3桶中,因为从我所读到的内容来看,这似乎更有效。

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

代码语言:javascript
运行
复制
  $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之后发送关于s3承诺的更新。

这似乎不太有效。

我可以创建一个guid并将其分配给s3文件名,还可以在酿酒厂对象上保留对guid的引用。不过,这听起来很烦人。

示例Guid创建者:

代码语言:javascript
运行
复制
    function guid() {
  function s4() {
    return Math.floor((1 + Math.random()) * 0x10000)
      .toString(16)
      .substring(1);
  }
  return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
    s4() + '-' + s4() + s4() + s4();
}

要达到我想要的目标,最干净的方法是什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-05-12 23:56:28

一个好的GUID生成器是解决一个独特的id问题的一个非常标准的方法。根据算法的不同,名称冲突的几率可能接近于零。正如您所知,JavaScript没有本地的,所以像您这样的人是合理的。我一点也不觉得这很讨厌。

这是@briguy37 37的另一篇

代码语言:javascript
运行
复制
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; };
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37198038

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档