直接从 Google Cloud Storage 读取文本文件时缓冲区出现奇怪的字符
Weird characters from Buffer when reading text file directly from Google Cloud Storage
直接从 Google Cloud Storage
读取文本文件时缓冲区中出现奇怪的字符
我一直在尝试各种编码并通过 express.js 发送 headers 但我无法弄清楚。我的问题是总是有几个字符是带问号的方块。如果我下载文件,它看起来不错。当我尝试直接读取文件时,它运行不正常并显示方块字符。我相信缓冲区中出现了问题。有没有人有直接从 Google 云存储中读取非英语文本文件的经验?
/* GET thing */
router.get('/thing/:filename', async function (req, res, next) {
var filename = req.params.filename
// works like a charm. i can open up the text file manually if I want and it looks good
storage.bucket('mybucket')
.file("folder/" + filename).download({
destination: "./" + filename
});
// here is the trouble though, i can not get the stream to work
console.log('Reading File');
res.header("Content-Type", "application/json; charset=utf-8");
var archivo = storage.bucket('mybucket')
.file("folder/" + filename).createReadStream();
console.log('Concat Data');
var buf = '';
archivo.on('data', function (d) {
buf += d;
}).on('end', function () {
console.log("End");
// print out the string
// the string also prints out the weird characters in the console btw.
res.json(buf);
}).on('error', function (e) {
res.json(e)
});
});
我很幸运在发布问题后很快就找到了答案。
不确定是什么导致了问题,但我最终使用了 string_decoder。我认为文件中有不同版本的 UTF8(不确定),但这就是为什么我不得不(再次)将其解码为 utf8 的原因。
var StringDecoder = require('string_decoder').StringDecoder;
var decoder = new StringDecoder('utf8');
原来是这样的
console.log('Concat Data');
var buf = '';
var decoder = new StringDecoder('utf8');
archivo.on('data', function (d) {
buf += decoder.write(d);
}).on('end', function () {
console.log("End");
console.log(buf)
res.json(buf);
}).on('error', function (e) {
res.json(e)
});
直接从 Google Cloud Storage
读取文本文件时缓冲区中出现奇怪的字符我一直在尝试各种编码并通过 express.js 发送 headers 但我无法弄清楚。我的问题是总是有几个字符是带问号的方块。如果我下载文件,它看起来不错。当我尝试直接读取文件时,它运行不正常并显示方块字符。我相信缓冲区中出现了问题。有没有人有直接从 Google 云存储中读取非英语文本文件的经验?
/* GET thing */
router.get('/thing/:filename', async function (req, res, next) {
var filename = req.params.filename
// works like a charm. i can open up the text file manually if I want and it looks good
storage.bucket('mybucket')
.file("folder/" + filename).download({
destination: "./" + filename
});
// here is the trouble though, i can not get the stream to work
console.log('Reading File');
res.header("Content-Type", "application/json; charset=utf-8");
var archivo = storage.bucket('mybucket')
.file("folder/" + filename).createReadStream();
console.log('Concat Data');
var buf = '';
archivo.on('data', function (d) {
buf += d;
}).on('end', function () {
console.log("End");
// print out the string
// the string also prints out the weird characters in the console btw.
res.json(buf);
}).on('error', function (e) {
res.json(e)
});
});
我很幸运在发布问题后很快就找到了答案。
不确定是什么导致了问题,但我最终使用了 string_decoder。我认为文件中有不同版本的 UTF8(不确定),但这就是为什么我不得不(再次)将其解码为 utf8 的原因。
var StringDecoder = require('string_decoder').StringDecoder;
var decoder = new StringDecoder('utf8');
原来是这样的
console.log('Concat Data');
var buf = '';
var decoder = new StringDecoder('utf8');
archivo.on('data', function (d) {
buf += decoder.write(d);
}).on('end', function () {
console.log("End");
console.log(buf)
res.json(buf);
}).on('error', function (e) {
res.json(e)
});