2013-09-24 134 views
4

我試圖在服務器端通過它的url從網頁中獲取圖像(即http://www.skrenta.com/images/stackoverflow.jpg),並使用Meteor將此圖像保存到我的AWS S3存儲桶中,aws- sdk隕石包以及http流星包。Meteor:將圖像從網址保存到AWS S3存儲

這是我的嘗試,它的確將一個文件放在我的存儲桶(someImageFile.jpg)中,但圖像文件已損壞,無法由瀏覽器或查看器應用程序顯示。

也許我做錯了文件的編碼。我嘗試了很多組合,但都沒有工作。另外,我嘗試添加的ContentLength和/或用不同的編碼方式,如二進制,十六進制ContentEncoding,基數64(也與Buffer.toString("base64")組合,他們沒有工作。任何建議,將不勝感激!

這是在我的服務器端-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,但對於這個問題,這是證明實際的最低例子問題

回答

0

最好的解決辦法是看一個噸什麼已經在這方面做得:

https://github.com/Lepozepo/S3

而且filepicker.so看起來很簡單:

Integrating Filepicker.IO with Meteor

+0

嘿FredericoC,謝謝你的輸入,這些源是非常好的,但沒有解決的情況下。我會更新我的帖子,使其更清楚地表明它是服務器端的推送,不涉及任何客戶端。 –

6

幾個試錯誤運行我放棄了Meteor.HTTP放在一起後下面的代碼,也許它會幫助有人遇到與Meteor.HTTP編碼問題。

Meteor.HTTP似乎只是爲了從遠程API中獲取一些JSON或文本數據等,不知怎的,它似乎並不安靜的選擇二進制數據。然而,故宮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)); 
      } 
     }); 
    }); 
});