stream uploading an gm-resized image to s3 with aws-sdk

前端 未结 1 1920
后悔当初
后悔当初 2020-12-23 15:41

so what i want to do is to stream an image from a url, process it with graphicsmagick and stream-upload it to s3. i just dont get it working.

streaming the processed

相关标签:
1条回答
  • 2020-12-23 15:52

    you don't need to read the stream yourself (in your case you seem to be converting from binary to string and back due to var str='' and then appending data which is a binary buffer etc...

    Try letting putObject pipe the stream like this:

    gm(request('http://www.some-domain.com/some-image.jpg'), "my-image.jpg")
      .resize("100^", "100^")
      .stream(function(err, stdout, stderr) {
          var data = {
            Bucket: "my-bucket",
            Key: "my-image.jpg",
            Body: stdout
            ContentType: mime.lookup("my-image.jpg")
          };
          s3.client.putObject(data, function(err, res) {
            console.log("done");
          });
        });
      });
    

    See these release notes for more info.

    If streaming/pipe doesn't work then something like this might which will load everything into memory and then upload. You're limited to 4Mb I think in this case.

        var buf = new Buffer('');
        stdout.on('data', function(data) {
           buf = Buffer.concat([buf, data]);
        });
        stdout.on('end', function(data) {
          var data = {
            Bucket: "my-bucket",
            Key: "my-image.jpg",
            Body: buf,
            ContentType: mime.lookup("my-image.jpg")
          };
          s3.client.putObject(data, function(err, res) {
            console.log("done");
          });
        });
    
    0 讨论(0)
提交回复
热议问题