如何使一个套接字stream? imagemagick后连接https响应S3
我一般都是节点和编程,而且我一直在努力…
我想采取一个https响应,调整与graphicsmagick它,并将其发送到我的亚马逊S3桶。
看来,https res是一个IncomingMessage对象(我找不到任何有关信息),而来自graphicsmagick的stdout是一个Socket。
奇怪的是,我可以使用pipe道,并发送这两个到本地path的writeStream,res和stdout创build一个不错的新的resize的图像。
而且我甚至可以将资源发送到S3(使用knox),它可以工作。
但标准输出不想去S3: – /
任何帮助,将不胜感激!
https.get(JSON.parse(queryResponse).data.url,function(res){ var headers = { 'Content-Length': res.headers['content-length'] , 'Content-Type': res.headers['content-type'] } graphicsmagick(res) .resize('50','50') .stream(function (err, stdout, stderr) { req = S3Client.putStream(stdout,'new_resized.jpg', headers, function(err, res){ }) req.end() }) })
knox – 连接到S3 – https://github.com/LearnBoost/knox graphicsmagick – 用于image processing – https://github.com/aheckmann/gm
问题出在事实上,亚马逊需要事先知道内容的长度(感谢DarkGlass)
但是,由于我的图像相对较小,我发现caching优先MultiPartUpload。
我的解决scheme
https.get(JSON.parse(queryResponse).data.url,function(res){ graphicsmagick(res) .resize('50','50') .stream(function (err, stdout, stderr) { ws. = fs.createWriteStream(output) i = [] stdout.on('data',function(data){ i.push(data) }) stdout.on('close',function(){ var image = Buffer.concat(i) var req = S3Client.put("new-file-name",{ 'Content-Length' : image.length ,'Content-Type' : res.headers['content-type'] }) req.on('response',function(res){ //prepare 'response' callback from S3 if (200 == res.statusCode) console.log('it worked') }) req.end(image) //send the content of the file and an end }) }) })
你不应该在那里做req.end()
。 通过这样做,您将在已经有时间发送图像数据之前将streamclosures到S3。 当所有的图像数据已经发送时,它将自动end
。
- 所需的节点/快递模块不更新与chokidar?
- Socket.io和OpenShift上的节点
- 从JSON创build一个mongoose模式
- i18next正在改变resGetPath
- 错误:require.paths被删除。 改用node_modules文件夹或NODE_PATH环境variables
- expressjs req.body.username未定义
- nodejitsu 400错误:ENOENT,打开'/opt/run/snapshot/package/images/tmp/72118-89rld0.png
- nodejs基于路由expressiondynamic静态文件夹
- Expressjs app.render不起作用