【发布时间】:2014-06-29 05:21:27
【问题描述】:
我正在使用 knox 包连接我的 S3 帐户并拉取图像,如下所示:
var picturestring;
knoxclient.get(key).on('response', function(res){
console.log(res.statusCode);
console.log(res.headers);
res.setEncoding('base64');
res.on('data', function(chunk){
picturestring += chunk;
});
res.on('end', function () {
console.log(picturestring);
resizeimage(picturestring, done); // use the resize library in this function
});
}).end();
之后,我想使用一个库来接收该字符串(图片字符串),调整图像大小,并返回一个新的 base64 字符串来表示调整后的图像。此时,我打算将调整大小的图像上传到 S3。
我在 Golang 中编写了一个类似的脚本,让我可以像这样调整图像大小,但是我查看过的每个 JS 大小调整库都只提供了从本地文件系统调整图像大小的示例。
有什么办法可以避免将图像从 S3 读取到文件系统中,而专注于专门处理返回的字符串??
***************更新****************************
function pullFromS3 (key, done) {
console.log("This is the key being pulled from Amazon: ", key);
var originalstream = new MemoryStream(null, {readable: false});
var picturefile;
client.get(key).on('response', function(res){
console.log("This is the res status code: ", res.statusCode);
res.setEncoding('base64');
res.pipe(originalstream);
res.on('end', function () {
resizeImage(originalstream, key, done);
});
}).end();
};
function resizeImage (originalstream, key, done) {
console.log("This is the original stream: ", originalstream.toString());
var resizedstream = new MemoryStream(null, {readable: false});
var resize = im().resize('160x160').quality(90);
// getting stuck here ******
originalstream.pipe(resize).pipe(resizedstream);
done();
};
我似乎无法掌握从 originalstream --> 到 resize ImageMagick 函数 ---> 到 resizestream 的管道是如何工作的。理想情况下,resizestream 应该包含调整大小图像的 base64 字符串,然后我可以将其上传到 S3。
1) 如何等待管道完成,然后使用 resizedstream 中的数据?
2) 我的管道是否正确?我无法调试它,因为我不确定如何等待管道完成!
【问题讨论】:
标签: node.js npm image-resizing