我正在通过前端将我的文件发送到一个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之后发送关于s3承诺的更新。
这似乎不太有效。
我可以创建一个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();
}要达到我想要的目标,最干净的方法是什么?
发布于 2016-05-12 23:56:28
一个好的GUID生成器是解决一个独特的id问题的一个非常标准的方法。根据算法的不同,名称冲突的几率可能接近于零。正如您所知,JavaScript没有本地的,所以像您这样的人是合理的。我一点也不觉得这很讨厌。
这是@briguy37 37的另一篇
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; };https://stackoverflow.com/questions/37198038
复制相似问题