[英]How to read content of JSON file uploaded to google cloud storage using node js
I manually upload the JSON file to google cloud storage by creating a new project.我通过创建一个新项目手动将 JSON 文件上传到谷歌云存储。 I am able to read the metadata for a file but I don't know how to read the JSON content.我能够读取文件的元数据,但我不知道如何读取 JSON 内容。
The code I used to read the metadata is:我用来读取元数据的代码是:
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);
});
Can someone guide me to the way to read the JSON file content?有人可以指导我阅读JSON文件内容的方法吗?
I've used the following code to read a json file from Cloud Storage:我使用以下代码从 Cloud Storage 读取 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);
});
};
I'm reading from a stream and concat all the data within the file to the buf variable.我正在从流中读取并将文件中的所有数据连接到 buf 变量。
Hope it helps.希望能帮助到你。
UPDATE更新
To read multiple files:读取多个文件:
'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");
});
});
};
There exists a convenient method:'download' to download a file into memory or to a local destination.有一种方便的方法:“下载”将文件下载到内存或本地目的地。 You may use download method as follows:您可以使用以下下载方法:
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);
});
A modern version of this:这个的现代版本:
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)
}
})()
I was using the createWriteStream
method like the other answers but I had a problem with the output in that it randomly output invalid characters ( ) for some characters in a string.我像其他答案一样使用createWriteStream
方法,但输出有问题,因为它随机输出字符串中某些字符的无效字符 ( )。 I thought it could be some encoding problems.我认为这可能是一些编码问题。
I came up with my workaround that uses the download
method.我想出了使用download
方法的解决方法。 The download
method returns a DownloadResponse
that contains an array of Buffer. download
方法返回一个包含 Buffer 数组的DownloadResponse
。 We then use Buffer.toString()
method and give it an encoding of utf8
and parse the result with JSON.parse()
.然后我们使用Buffer.toString()
方法并给它一个utf8
编码并使用JSON.parse()
解析结果。
const downloadAsJson = async (bucket, path) => {
const file = await new Storage()
.bucket(bucket)
.file(path)
.download();
return JSON.parse(file[0].toString('utf8'));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.