简体   繁体   English

如何使用node js读取上传到谷歌云存储的JSON文件的内容

[英]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.

相关问题 如何在 React / next.js 上读取上传的 json 文件的内容 - How to read content of uploaded json file on react / next.js Node.js:获取上传文件的内容以创建读取流 - Node.js : get content of uploaded file for creating read stream 使用 Node.js 将 pdf 文件上传到 Google Cloud Storage - Upload a pdf file to Google Cloud Storage with Node.js 使用节点js创建预签名网址并将文件上传到Google云端存储 - create a pre signed url and upload a file to google cloud storage using node js 如何使用 Node.js 在 Google Cloud Storage 中更改文件的元数据 - How to change file's metadata in Google Cloud Storage with Node.js 如何使用 google-cloud 和 node js 从 firebase 存储中检索图像并将其显示在网页上 - how to retrieve image from firebase storage and display it on a webpage using google-cloud and node js 如何使用 Node.js 将 Blob base64 字符串上传到谷歌云存储 - How do you upload a blob base64 string to google cloud storage using Node.js 如何在Node JS中测试上传的文件是否为json文件? - How to test if the uploaded file is a json file in Node JS? 如何从@google-cloud/storage 读取文件? - How to read file from @google-cloud/storage? Node Js:如何读取和更新 json 文件 - Node Js : How to read and update json file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM