【发布时间】:2016-09-08 22:40:02
【问题描述】:
我正在通过前端将我的文件发送到一个 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 = {};
});
}
};
上面的代码不起作用,除非我在 distillery 对象创建后和文件上传到 s3 后发送了关于 aws.Upload 承诺的更新。
这似乎没有效率。
我可以创建一个 guid 并将其分配给 s3 文件名,并在 distillery 对象上保留对它的引用。不过,这似乎很hacky。
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();
}
实现我想要的最干净的方法是什么?
【问题讨论】:
-
你可能想研究使用像fineuploader这样的包
标签: angularjs amazon-web-services amazon-s3 mongoose