我是一名 Python 开发人员,但我现在正在从事的项目的情况迫使我在 Node.js 中找到解决方案。
我已经检查了文档在类File中,我有这个方法:createReadStream,但谁强迫我在读取之前先下载到本地。
然而,我搜索的解决方案就像将内容保存在变量中,以便我可以根据需要读取和解释。
这是createReadStream()方法的脚本:
var storage = require('@google-cloud/storage')();
var bucket = storage.bucket('my-bucket');
var fs = require('fs');
var remoteFile = bucket.file('image.png');
var localFilename = '/Users/stephen/Photos/image.png';
remoteFile.createReadStream()
.on('error', function(err) {})
.on('response', function(response) {
// Server connected and responded with the specified status
})
.on('end', function() {
// The file is fully downloaded.
})
.pipe(fs.createWriteStream(localFilename));
感谢您的理解和帮助。
Script:
const {Storage} = require('@google-cloud/storage');
const storage = new Storage();
const bucket = storage.bucket(bucket);
const remoteFile = bucket.file(file);
let buffer = '';
remoteFile.createReadStream()
.on('error', function(err) {console.log(err)})
.on('data', function(response) {
buffer += response
})
.on('end', function() {
//console.log(buffer);
res.send(buffer);
})
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)