流星 如何通过 REST API 将图片上传到 AWS s3?



我正在研究 api 以在 aws s3 上上传图像。首先,我需要将图像保存到aws,然后需要将url保存到mongo数据库。但是当我尝试上传图像时出现此错误。我在clinet端使用lepozepo:s3库,并希望在服务器端使用相同的库。我在下面提到的代码是带有lepozepo库的。 http://prntscr.com/ohareb

Meteor.call('uploadProfilePic', this.request.body, function (error, resp) {
if (error) {
response = {
"errorCode": true,
"statusMessage": error.message,
} 
}else{
response = {
"errorCode": false,
"statusMessage": "Picture uploaded successfully",
}
}
});
this.response.setHeader('Content-Type', 'application/json');
this.response.end(JSON.stringify(response));

});

在方法中,我创建了这种方法。

uploadProfilePic:function(image){
var files = image;
userId = "MMKKK79KQ7eMs6777Hh";
import {S3} from "meteor/lepozepo:s3";
console.log(files);
S3.uploadFile({ 
files:files,
path:"avatars"
},function(e,r){
if (!e) {
var $set={};
$set[templ.data.picType]=r.secure_url;
Meteor.users.update({
_id: userId
}, {
$set: $set
}, function(err) {
if (!err) {
console.log( "Image uploaded");
} else {
console.log("Error updating image");
}
});
} else {
console.log( "Error updating image");
}
});
}

我维护这个包:https://github.com/activitree/s3up-meta。 有了它,您可以在 S3 中上传和删除文件。您还可以设置用于缓存和过期的元数据,无论如何,您都可以从 Cloudfront CDN 覆盖这些元数据(以防您通过 Cloudfront 分发映像)。 该软件包基于 AWS 开发工具包,所有请求都由 Meteor 服务器签名,但是,文件从客户端直接移动到 S3。 如果需要,请在 Git 上打开一个问题。 干杯

最新更新