问题描述
我试图使用Azure的SDK换节点到流图像保存到Windows Azure Blob存储,但没有成功。下面是我打电话和缩略图财产传视频对象的函数。起初,我用获取从其他网站获取的图像,并把它转换成这反过来又被转换成流对象一个base64对象,因为Azure的BLOB服务使用createBlockBlobFromStream方法,我不能使用createBlockBlobFromFile或createBlockBlobFromText上传请求对象的图像图像以Blob存储。
I am trying to use the azure-sdk-for-node to save a streamed image to Windows Azure blob storage but without success. Below is the function that I call and pass the video object with the thumbnail property. Initially I fetch the image using the request object which fetches the image from another website and turn that into a base64 object which in turn gets converted into a stream object because Azure blob service uses createBlockBlobFromStream method as I couldn't use createBlockBlobFromFile or createBlockBlobFromText to upload the image to blob storage.
var azure = require('azure')
, uuid = require('node-uuid')
, http = require('http')
, url = require('url')
, mmm = require('mmmagic')
, Magic = mmm.Magic
, stream = require('stream');
function createVideoThumbnail(video, callback){
var bs = azure.createBlobService(config.storageAccount, config.storageAccessKey, config.blobHost);
var sURL = video.Thumbnail;
var oURL = url.parse(sURL);
var client = http.createClient(80, oURL.hostname);
var request = client.request('GET', oURL.pathname, {'host': oURL.hostname});
request.end();
request.on('response', function (response) {
var type = response.headers["content-type"];
var prefix = "data:" + type + ";base64,";
var body = "";
response.setEncoding('binary');
response.on('end', function () {
var base64 = new Buffer(body, 'binary').toString('base64');
var data = prefix + base64;
console.log('base64 image data ' + video.Thumbnail + ': ' + data + '\n');
var decodedImage = new Buffer(data, 'base64');
var magic = new Magic(mmm.MAGIC_MIME_TYPE);
magic.detect(decodedImage, function(err, result) {
if(err) {
throw err;
}
var bytes = 0;
var imageStream = new stream.Stream();
imageStream.writable = true;
imageStream.write = function(buf) {
bytes += buf.length;
imageStream.emit('data', buf);
};
imageStream.end = function(buf) {
//if(arguments.length) {
imageStream.write(buf);
//}
imageStream.writable = false;
imageStream.emit('end');
console.log(bytes + ' bytes written');
};
var options = {}
console.log('mmm = ' + result + '\n');
options.contentType = result;
options.contentTypeHeader = result;
console.log('\n');
bs.createBlockBlobFromStream(config.imageContainer, uuid().replace(/-/gi, "").toLowerCase() + '.jpg', imageStream, decodedImage.length, options, function(error, blobResult, response) {
if (error)
console.log('got error = ' + JSON.stringify(error) + '\n');
if (blobResult)
console.log('blobResult = ' + JSON.stringify(blobResult) + '\n');
if (response)
console.log('response = ' + JSON.stringify(response) + '\n');
// now store in Azure blob storage
callback();
});
imageStream.end(decodedImage);
});
});
response.on('data', function (chunk) {
if (response.statusCode == 200) body += chunk;
});
});
}
这就是我怎么称呼它:
and this is how I call it:
createVideoThumbnail(video, function(){
console.log("returning from create thumbnails\n\n");
});
该功能无法正常工作搞得晕头转向,不会打印出来的,最终的日志语句:
The function is not working it hangs and won't print out the the final log statement:
console.log("returning from create thumbnails\n\n");
然而以base64似乎因为我得到这个的编码工作:
However the base64 does seem to work as I am getting this for the encoding:
MMM =应用程序/八位字节流
书面5283字节
mmm = application/octet-stream5283 bytes written
但我没有得到任何这些日志报表正在打印:
But I am not getting any of these log statements being printed:
if (error)
console.log('got error = ' + JSON.stringify(error) + '\n');
if (blobResult)
console.log('blobResult = ' + JSON.stringify(blobResult) + '\n');
if (response)
console.log('response = ' + JSON.stringify(response) + '\n');
所以我presuming它挂的地方还是我没有结构性我的code正确。任何人都可以看到我在做什么错了?
So I am presuming it is hanging somewhere or I have not structured my code properly. Can anybody see what I am doing wrong ?
干杯
罗布
我的消息来源:
http://social.msdn.microsoft.com/Forums/en-US/wavirtualmachinesforlinux/thread/47bfe142-c459-4815-b09e-bd0a07ca18d5
<一个href=\"http://stackoverflow.com/questions/3709391/node-js-base64-en$c$c-a-downloaded-image-for-use-in-data-uri\">Node.js连接的base64 code在数据使用下载的图像URI
推荐答案
由于瓦列里·雅各布斯在MSDN论坛能够想出答案。这是使用流,请求和util对象一个干净的固溶体。
Thanks to Valery Jacobs on msdn forums was able to come up with the answer. It's a nice clean solid solution using the stream, request and util object.
:)
http://social.msdn.microsoft.com/Forums/en-US/windowsazurepurchasing/thread/25c7705a-4ea0-4d9c-af09-cb48a031d06c
这篇关于创建使用的Windows Azure Blob存储缩略图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!