Meteor: Saving images from urls to AWS S3 storage

北慕城南 提交于 2019-12-05 17:28:30

After several trial an error runs I gave up on Meteor.HTTP and put together the code below, maybe it will help somebody when running into encoding issues with Meteor.HTTP.

Meteor.HTTP seems to be meant to just fetch some JSON or text data from remote APIs and such, somehow it seems to be not quiet the choice for binary data. However, the Npm http module definitely does support binary data, so this works like a charm:

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));
            }
        });
    });
});
FredericoC

The best solution is to look at what has already been done in this regard:

https://github.com/Lepozepo/S3

Also filepicker.so seems pretty simple:

Integrating Filepicker.IO with Meteor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!