2012-04-20 12 views
1

私のノードjsコードは、自分のサーバーからローカルのpngファイルtmp.pngを開き、amazon S3を保存しようとします。私は問題を抱え続けており、エンコーディングと関係があると思われます。それがうまく動作する唯一の方法は、base64エンコーディング(私は自分の写真には望ましくない)です。awssumとnodejsの画像をamazonに保存するS3

fs = require('fs'); 
var awssum = require('awssum'); 
var amazon = awssum.load('amazon/amazon'); 
var s3Service = awssum.load('amazon/s3'); 

var s3 = new s3Service('mykey', 'mysecret', 'account', amazon.US_WEST_1); 

fs.readFile('./tmp.png', function (err, data){ 
    if(err){ 
     console.log("There was an error opening the file"); 
    } else { 
     s3.PutObject({ 
      BucketName : 'my-bucket', 
      ObjectName : 'tmp.png', 
      ContentType : 'image/png', 
      ContentLength : data.length, 
      Body   : data, 
     }, function(err, data) { 
      if(err){ 
       console.log("There was an error writing the data to S3:"); 
       console.log(err); 
      } else { 
       console.log("Your data has been written to S3:"); 
       console.log(data); 
      } 
     }); 
    } 

}); 

明らかに、my-bucketは実際には私の一意のバケット名です。私がアマゾンから返信したメッセージは、要求のタイムアウトです:

タイムアウト時間内にサーバーとのソケット接続が読み書きされませんでした。アイドル状態の接続は閉じられます。

+0

注このコードはv0.4がおよそからAwsSum(マイライブラリ)の古いバージョンを使っている:キーファイルを読み込むためのファイルサイズとfs.createReadStreamためfs.stat使用することでした。新しいバージョンv0.5以上では構文が少し異なるので、AwsSumに付属のexamples/folderを調べる価値があります。 – chilts

答えて

2

私はそれが必要なことを行うドキュメントの例が見つかりました。 、

// you must run fs.stat to get the file size for the content-length header (s3 requires this) 
fs.stat(path, function(err, file_info) { 
    if (err) { 
     inspect(err, 'Error reading file'); 
     return; 
    } 

    var bodyStream = fs.createReadStream(path); 

    console.log(file_info.size); 

    var options = { 
     BucketName : 'my-bucket', 
     ObjectName : 'test.png', 
     ContentType : 'image/png', 
     ContentLength : file_info.size, 
     Body   : bodyStream 
    }; 

    s3.PutObject(options, function(err, data) { 
     console.log("\nputting an object to my-bucket - expecting success"); 
     inspect(err, 'Error'); 
     inspect(data, 'Data'); 
    }); 
}); 
+0

上記のコードは私のためには機能しません。私は依然としてRequest Timeoutエラーを受けます。何か案が? –

関連する問題