node.js - 从 Google Logging API 解码 protoPayload

标签 node.js protocol-buffers google-api-nodejs-client google-cloud-logging

我想阅读 BigQuery 日志条目以进行一些分析。但我似乎无法对 protoPayload.value 进行解码。我试过弄乱 google-proto-filesprotocol-buffers 包,但我想我在这里遗漏了一些非常明显的东西......

const Logging = require('@google-cloud/logging');
const protobuf = require('protocol-buffers');
const protoFiles = require('google-proto-files');


const protoPath = './node_modules/google-proto-files/google/cloud/audit/audit_log.proto';
const root = protoFiles.loadSync(protoPath)
const AuditLog = root.lookup('google.cloud.audit.AuditLog');

const client = new Logging.v2.LoggingServiceV2Client({ projectId });
client.listLogEntriesStream({resourceNames, filter, pageSize})
    .on('data', entry => {
        console.log(entry); // Entry is of type AuditLog
        console.log(AuditLog.decode(entry.protoPayload.value.buffer));
        process.exit(1)
    })
    .on('error', e => console.error(e))
    .on('end', () => console.info('END RECEIVED', arguments))

我确实收到了带有 protoPayloads 的消息,但是我在尝试解码消息时收到的错误是这样的:

Error: no such Type or Enum 'google.rpc.Status' in Type .google.cloud.audit.AuditLog

实际问题:解码 LogEntry 中的 protoPayload 字段的正确方法是什么?

谢谢!

最佳答案

由于 entry.protoPayload.value 是一个序列化的原型(prototype)(一条 AuditLog 消息),您应该能够使用记录在 https://developers.google.com/protocol-buffers/docs/reference/javascript-generated#message 中的 deserializeBinary() 方法来处理它‘protocol-buffers’ npm 似乎不是来自 Google,并且 proto 编译器将生成用于反序列化的代码。

我认为您不需要,但您也可以尝试显式加载“google/rpc/status.proto”定义。

关于node.js - 从 Google Logging API 解码 protoPayload,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50935252/

相关文章:

javascript - 如何修复类型错误: registerUser is not a function

java - Google Protocol Buffer 和 UTF-16

c++ - 未定义的引用谷歌 Protocol Buffer 类

node.js - 如何在 Sails.js 中使用 Waterline 运行自定义查询?

javascript - 我怎样才能阻止不和谐的机器人回复自己或其他机器人? (discord.js)

javascript - 如何从带有单引号和大括号的 Web 服务接收的 JSON 对象中提取数据?

c++ - Google Protocol Buffer ,如何设置自定义类型的字段?

node.js - Google Calendar API V3 不尊重 Nodejs Express 应用程序中的 timeMin timeMax 参数

node.js - 如何在 Node.js 中使用谷歌标签管理器

javascript - 如何提高文件上传到谷歌存储的速度?