4

我正在尝试在服务器端通过它的 url(即http://www.skrenta.com/images/stackoverflow.jpg)从网络上获取一张图片,并使用 Meteor 将这张图片保存到我的 AWS S3 存储桶中,aws -sdk 陨石包以及 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,但对于这个问题,这是演示实际问题的最小示例。

4

2 回答 2

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));
            }
        });
    });
});
于 2013-09-26T07:21:08.947 回答
0

最好的解决方案是看看在这方面已经做了什么:

https://github.com/Lepozepo/S3

filepicker.so 看起来也很简单:

将 Filepicker.IO 与 Meteor 集成

于 2013-09-26T03:15:27.373 回答