我正在通过前端将文件发送到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没有本机语言,因此像您这样的人是合理的。我认为它一点也不怪异。
这是@ 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; };
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句