如何使一个套接字流?为了ImageMagick的后HTTPS响应连接到S3 [英] How to make a socket a stream? To connect https response to S3 after imagemagick
问题描述
我的节点和程序设计一般,我一直在真正与这个挣扎...
I am node and programming in general, and I have been really struggling with this...
我要带一个HTTPS响应,实:graphicsmagick调整其大小,并将其发送到我的Amazon S3的桶中。
I want to take a https response, resize it with graphicsmagick and send it to my amazon S3 bucket.
看来,HTTPS资源是IncomingMessage对象(我无法找到任何信息),并从实:graphicsmagick标准输出是一个插座。
It appears that the https res is an IncomingMessage object (I can't find any info about that) and the stdout from graphicsmagick is a Socket.
奇怪的是,我可以使用管道和这两个与本地路径发送到writeStream,双方资源和stdout创建一个不错的新调整后的图像。
The weird thing is that I can use pipe and send both of these to a writeStream with a local path, and both res and stdout create a nice new resized image.
和我甚至可以发送资源的S3(使用诺克斯)和它的作品。
And I can even send res to the S3 (using knox) and it works.
不过,标准输出并不想去的S3: - /
But stdout doesn't want to go to the S3 :-/
任何帮助将是AP preciated!
Any help would be appreciated!
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()
})
})
诺克斯 - 连接到S3 - https://github.com/LearnBoost/knox 实:graphicsmagick - 图像处理 - https://github.com/aheckmann/gm
knox - for connecting to S3 – https://github.com/LearnBoost/knox graphicsmagick - for image manipulation – https://github.com/aheckmann/gm
推荐答案
问题是与亚马逊需要知道前手的内容长度的事实(感谢DarkGlass)
The problem was with the fact that Amazon needs to know content length before hand (thanks DarkGlass)
不过,因为我的形象都比较小,我发现缓冲preferential到MultiPartUpload。
However, since my images are relatively small I found buffering preferential to MultiPartUpload.
我的解决办法:
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
})
})
})
这篇关于如何使一个套接字流?为了ImageMagick的后HTTPS响应连接到S3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!