2014-01-10 13 views
2

我正在使用Nodejs嘗試使用aws-sdk將圖像推送到S3實例。目前,它從客戶端的一個文件讀取,然後將其保存在服務器上(我使用的是流星框架)。我想將它推送到S3服務器,而不是將它保存在流星服務器上。當我嘗試將它遷移時,圖像似乎在S3上獲得30%左右。如果我嘗試從S3下載它們,圖像也不再可見,所以它看起來已經改變了編碼或其他東西。putObject使Nodejs服務器上的對象變大

這裏是加載在客戶端的文件中的代碼:

saveFile = function(blob, name, path, type, callback) { 
    var fileReader = new FileReader(); 
    var method; 
    var encoding = 'binary'; 
    var type = type || 'binary'; 

    switch(type) { 
    case 'text': 
     method = 'readAsText'; 
     encoding = 'utf8'; 
     break; 
    case 'binary': 
     method = 'readAsBinaryString'; 
     encoding = 'binary'; 
     break; 
    default: 
     method = 'readAsBinaryString'; 
     encoding = 'binary'; 
     break; 
    } 

    // Call the save function on the server after the file has been read. 
    fileReader.onload = function(file) { 
    console.log("File loaded..."); 
    Meteor.call('saveFile', file.srcElement.result, name, path, encoding, callback); 
    } 

    // Read the file 
    fileReader[ method ](blob); 
} 

在服務器端:

saveFile: function(file, name, path, encoding) { 
    s3.createBucket({Bucket: bucketName}, function() { 
     var params = {Bucket: bucketName, Key: keyName, ContentType: 'binary', ContentEncoding: 'utf8', Body: file}; 
     s3.putObject(params, function(err, data) { 
     if (err) 
      console.log(err) 
     else 
      console.log("Successfully uploaded data to " + bucketName + "/" + keyName); 
     }); 
    }); 
+0

看起來像你的base64編碼它 – Bram

回答

2

我想出瞭解決方案,它是封裝的「文件」對象在一個

new Buffer() 

簡單,但很難找到哦!