gpt4 book ai didi

node.js - 将 gcloud createReadStream 加载到 nodejs 中的变量中

转载 作者:搜寻专家 更新时间:2023-11-01 00:14:56 26 4
gpt4 key购买 nike

我正在使用 nodejs 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 第 3 方库团队慷慨地同意添加一种新方法来简化请求:

https://github.com/GoogleCloudPlatform/gcloud-node/pull/381

更新:

该函数现在在 gcloud 库中可用。

关于node.js - 将 gcloud createReadStream 加载到 nodejs 中的变量中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28256033/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com