Ara*_*ndh 7 javascript google-api node.js 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('project-name')
.file('file-name')
.getMetadata()
.then(results => {
console.log("results is", results[0])
})
.catch(err => {
console.error('ERROR:', err);
});
有人可以指导我阅读JSON文件内容的方法
F10*_*F10 10
我使用以下代码从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);
});
};
Run Code Online (Sandbox Code Playgroud)
我正在从流中读取并将文件中的所有数据合并到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");
});
});
};
Run Code Online (Sandbox Code Playgroud)
Pra*_*ngh 10
有一种方便的方法:“下载”将文件下载到内存或本地目的地。您可以使用以下下载方法:
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);
});
Run Code Online (Sandbox Code Playgroud)
这个的现代版本:
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)
}
})()
Run Code Online (Sandbox Code Playgroud)
我createWriteStream像其他答案一样使用该方法,但我的输出有问题,因为它随机输出字符串中某些字符的无效字符(?)。我认为这可能是一些编码问题。
我想出了使用该download方法的解决方法。该download方法返回一个DownloadResponse包含一个 Buffer 数组。然后我们使用Buffer.toString()method 并给它一个编码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'));
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4111 次 |
| 最近记录: |