我正在服务器端尝试通过它的网址(即http://www.skrenta.com/images/stackoverflow.jpg)从网络上获取图像,然后使用Meteor,aws-sdk陨石包以及此图像将此图像保存到我的AWS S3存储桶中http流星包。
这是我的尝试,它确实将文件放入我的存储桶(someImageFile.jpg),但图像文件已损坏,无法由浏览器或查看器应用程序显示。
可能我对文件的编码做错了。我尝试了很多组合,但没有一个合作。此外,我尝试使用不同的编码添加ContentLength和/或ContentEncoding,如二进制,十六进制,base64(也与Buffer.toString("base64")
结合使用,没有一个工作。任何建议将不胜感激!
这是我的服务器端代码:
var url="http://www.skrenta.com/images/stackoverflow.jpg";
HTTP.get(url, function(err, data) {
if (err) {
console.log("Error: " + err);
} else {
//console.log("Result: "+JSON.stringify(data));
//uncommenting above line fills up the console with raw image data
s3.putObject({
ACL:"public-read",
Bucket:"MY_BUCKET",
Key: "someImageFile.jpg",
Body: new Buffer(data.content,"binary"),
ContentType: data.headers["content-type"], // = image/jpeg
//ContentLength: parseInt(data.headers["content-length"]),
//ContentEncoding: "binary"
},
function(err,data){ // CALLBACK OF HTTP GET
if(err){
console.log("S3 Error: "+err);
}else{
console.log("S3 Data: "+JSON.stringify(data));
}
}
);
}
});
实际上我试图通过HTTP调用来使用filepicker.io REST API,即将转换后的图像存储到我的s3中,但是对于这个问题,这是演示实际问题的最小例子。
答案 0 :(得分:6)
经过多次试用后出现错误,我放弃了Meteor.HTTP
并将下面的代码放在一起,也许这会在遇到Meteor.HTTP
编码问题时帮助某人。
Meteor.HTTP
似乎只是想从远程API获取一些JSON或文本数据等等,不知怎的,二进制数据的选择似乎并不安静。但是,Npm http模块肯定支持二进制数据,所以这就像一个魅力:
var http=Npm.require("http");
url = "http://www.whatever.com/check.jpg";
var req = http.get(url, function(resp) {
var buf = new Buffer("", "binary");
resp.on('data', function(chunk) {
buf = Buffer.concat([buf, chunk]);
});
resp.on('end', function() {
var thisObject = {
ACL: "public-read",
Bucket: "mybucket",
Key: "myNiceImage.jpg",
Body: buf,
ContentType: resp.headers["content-type"],
ContentLength: buf.length
};
s3.putObject(thisObject, function(err, data) {
if (err) {
console.log("S3 Error: " + err);
} else {
console.log("S3 Data: " + JSON.stringify(data));
}
});
});
});
答案 1 :(得分:0)