我正在使用Node.js API进行云存储。您可以参考https://github.com/GoogleCloudPlatform/gcloud-node。我遇到了使用createReadStream()将文件作为字符串加载的困难。虽然有一个在本地环境中管道化文件并写入的示例,但是我更喜欢将其加载到变量中。请帮助我解决这个问题。
供以后参考,这是我解决问题的方法。连续从缓冲区获取数据。
var gcloud = require("gcloud")({ /* credentials. */ });
var cloudstorage = gcloud.storage();
var myTextFile = cloudstorage.file('your-file.txt');
var fileContents = new Buffer('');
myTextFile.createReadStream()
.on('data', function(chunk) {
fileContents = Buffer.concat([fileContents, chunk]);
})
.on('end', function() {
// `fileContents` is ready
});
gCloud第三方库团队慷慨地同意添加一个新的方法来简化请求:
https://github.com/GoogleCloudPlatform/gcloud-node/pull/381
更新:
该函数现在已经在gcloud库中可用。