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
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");
});
});