Javascript 如何使用node js读取上传到google云存储的JSON文件内容
我通过创建一个新项目手动将JSON文件上传到google云存储。我能够读取文件的元数据,但不知道如何读取JSON内容 我用来读取元数据的代码是:Javascript 如何使用node js读取上传到google云存储的JSON文件内容,javascript,node.js,google-api,google-cloud-storage,Javascript,Node.js,Google Api,Google Cloud Storage,我通过创建一个新项目手动将JSON文件上传到google云存储。我能够读取文件的元数据,但不知道如何读取JSON内容 我用来读取元数据的代码是: var Storage = require('@google-cloud/storage'); const storage = Storage({ keyFilename: 'service-account-file-path', projectId: 'project-id' }); storage .bucket('proj
var Storage = require('@google-cloud/storage');
const storage = Storage({
keyFilename: 'service-account-file-path',
projectId: 'project-id'
});
storage
.bucket('project-name')
.file('file-name')
.getMetadata()
.then(results => {
console.log("results is", results[0])
})
.catch(err => {
console.error('ERROR:', err);
});
有人能告诉我如何读取JSON文件内容吗?我使用以下代码从云存储读取JSON文件:
'use strict';
const Storage = require('@google-cloud/storage');
const storage = Storage();
exports.readFile = (req, res) => {
console.log('Reading File');
var archivo = storage.bucket('your-bucket').file('your-JSON-file').createReadStream();
console.log('Concat Data');
var buf = '';
archivo.on('data', function(d) {
buf += d;
}).on('end', function() {
console.log(buf);
console.log("End");
res.send(buf);
});
};
我正在从一个流中读取数据,并将文件中的所有数据连接到buf变量
希望能有帮助
更新
要读取多个文件,请执行以下操作:
'use strict';
const {Storage} = require('@google-cloud/storage');
const storage = new Storage();
listFiles();
async function listFiles() {
const bucketName = 'your-bucket'
console.log('Listing objects in a Bucket');
const [files] = await storage.bucket(bucketName).getFiles();
files.forEach(file => {
console.log('Reading: '+file.name);
var archivo = file.createReadStream();
console.log('Concat Data');
var buf = '';
archivo.on('data', function(d) {
buf += d;
}).on('end', function() {
console.log(buf);
console.log("End");
});
});
};
存在一种方便的方法:“下载”将文件下载到内存或本地目标。您可以使用以下下载方法:
const bucketName='bucket name here';
const fileName='file name here';
const storage = new Storage.Storage();
const file = storage.bucket(bucketName).file(fileName);
file.download(function(err, contents) {
console.log("file err: "+err);
console.log("file data: "+contents);
});
现代版:
const { Storage } = require('@google-cloud/storage')
const storage = new Storage()
const bucket = storage.bucket('my-bucket')
// The function that returns a JSON string
const readJsonFromFile = async remoteFilePath => new Promise((resolve, reject) => {
let buf = ''
bucket.file(remoteFilePath)
.createReadStream()
.on('data', d => (buf += d))
.on('end', () => resolve(buf))
.on('error', e => reject(e))
})
// Example usage
(async () => {
try {
const json = await readJsonFromFile('path/to/json-file.json')
console.log(json)
} catch (e) {
console.error(e)
}
})()
与其他答案一样,我使用了
createWriteStream
方法,但输出有一个问题,它随机输出无效字符(�) 对于字符串中的某些字符。我认为可能是编码问题
我提出了使用download
方法的解决方法。download
方法返回一个downloadsresponse
,其中包含一个缓冲区数组。然后我们使用Buffer.toString()
方法,给它一个utf8
编码,并用JSON.parse()
解析结果
const downloadAsJson=async(bucket,path)=>{
const file=等待新存储()
.桶(桶)
.file(路径)
.download();
返回JSON.parse(文件[0].toString('utf8');
}
从未使用过此功能,但您似乎需要将其下载到memaybe。如果您正在阅读一个文件,则此功能可以帮助您。但是如果您想阅读多个文件,该怎么办。谢谢。看起来就像我最初使用的一样。但您是否测试过此功能?createReadStream似乎正在进行一次异步冒险,结果是:D我不得不用awai欺骗它t、 …是的,那将取决于你的文件有多大。我要看多长时间才能有人找到一个有效的例子。谢谢!