流星:从保存图像的URL到AWS S3存储 [英] Meteor: Saving images from urls to AWS S3 storage

查看:228
本文介绍了流星:从保存图像的URL到AWS S3存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想,服务器端,拍摄图像从网页通过它的URL(即 http://www.skrenta.com/images/stackoverflow.jpg ),并使用流星的AWS-SDK陨石包还有HTTP流星包。这个图片保存到我的AWS S3存储

I am trying, server-side, to take an image from the web by it's url (i.e. http://www.skrenta.com/images/stackoverflow.jpg) and save this image to my AWS S3 bucket using Meteor, the aws-sdk meteorite package as well as the http meteor package.

这是我的企图,这确实把一个文件在我的桶(someImageFile.jpg),但图像文件被破坏然后,不能由浏览器或查看器应用程序显示的

This is my attempt, which indeed put a file in my bucket (someImageFile.jpg), but the image file is corrupted then and cannot be displayed by a browser or a viewer application.

大概是我做错了什么与该文件的编码。我试过很多的组合,其中没有工作。另外,我尝试添加CONTENTLENGTH和/或不同的编码方式,如二进制,十六进制ContentEncoding,基数64(也与 Buffer.toString组合(BASE64),没有一次成功。任何意见将大大AP preciated!

Probably I am doing something wrong with the encoding of the file. I tried many combinations and none of them worked. Also, I tried adding ContentLength and/or ContentEncoding with different encodings like binary, hex, base64 (also in combination with Buffer.toString("base64"), none of them worked. Any advice will be greatly appreciated!

这是在我的服务器副作用code:

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调用,即用于存储转换的图像到我的S3的filepicker.io REST API,但对于这个问题,这是起码的例子来演示实际的问题。

Actually I am trying to use the filepicker.io REST API via HTTP calls, i.e. for storing a converted image to my s3, but for this problem this is the minimum example to demonstrate the actual problem.

推荐答案

经过多次试验错误运行我放弃了 Meteor.HTTP 并放在一起code以下,也许这将与 Meteor.HTTP 运行到编码问题的时候帮助别人。

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 似乎意味着只是获取远程API和这样一些JSON或文本数据,不知何故,似乎是不安静的二进制数据的选择。然而,故宫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));
            }
        });
    });
});

这篇关于流星:从保存图像的URL到AWS S3存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆