如何从Node.js中的Google存储桶读取二进制文件?

时间:2020-05-09 09:55:10

标签: node.js google-cloud-platform google-cloud-storage binaryfiles hyperledger-fabric-sdk-js

有什么最好的方法可以从Google存储桶中读取二进制文件,我正在尝试以下代码,但是当我尝试从存储桶中读取文件并提取时,我面临着非法偏移问题。

我已经在fabriccerts存储桶下手动上传了扩展名为.tx的二进制文件。我必须阅读云函数中的文件,并使用fabrclient模块提取相同的文件。

以下是从存储桶读取文件的功能。

 const {Storage} = require('@google-cloud/storage');
    const storage = new Storage();
    async function getFileContent(fileName, bucketName) {
      const myBucket = storage.bucket(bucketName);
      const file = myBucket.file(fileName);
      const content = await file.download();
      return content;
    };

下面是我正在尝试使用fabric-client模块提取二进制文件的cloud函数中的语句。

const Client = require('fabric-client');
    let envelope_bytes = await storage_util.getFileContent("channel.tx", "fabriccerts");
   console.log("file data" + envelope_bytes);
    let config_update = client.extractChannelConfig(envelope_bytes);
Error: Error: function terminated. Recommended action: inspect logs for termination reason. Details:
Illegal offset: 0 <= 1 (+1) <= 1

是否有读取二进制文件的特定方法?下载后是否需要转换文件?

我在ubuntu机器中尝试过相同的操作,将channel.tx文件存储在本地目录中,该文件运行正常,并且在读取文件后显示在下面:

    �
                                                                                                                                                               ����"     mychannel�
    �
            mychannel;)

    Application

    Org1MSP

    Org2MSP

    Consortium��

    Application�

    Org2MSP

    Org1MSP&
    Capabilities



    V1_4_2Admins""
    WritersAdmins""
    Admins

    AdminsAdmins""
    ReadersAdmins*Admins"

    Consortium
    SampleConsortium

但是当我将相同的文件存储在存储桶中并读取文件时,它显示在数据下方:

    �  ����" mychannel� � mychannel;) Application Org1MSP� Org2MSP� Consortium��� Application� Org2MSP� Org1MSP�& Capabilities V1_4_2�Admins"" Writers  WritersAdmins"" Admins AdminsAdmins"" Readers  ReadersAdmins*Admins" Consortium SampleConsortium

错误日志:

RangeError: Illegal offset: 0 <= 1 (+1) <= 1 at ByteBuffer.module.exports.ByteBufferPrototype.readVarint32 (/srv/node_modules/bytebuffer/dist/bytebuffer-node.js:1540:23) at Message.ProtoBuf.Reflect.MessagePrototype.decode (/srv/node_modules/fabric-client/node_modules/protobufjs/dist/protobuf.js:3237:36) at Function.Message.decode (/srv/node_modules/fabric-client/node_modules/protobufjs/dist/protobuf.js:2952:37) at Client.extractChannelConfig (/srv/node_modules/fabric-client/lib/Client.js:660:42) at Object.exports.createChannel (/srv/chainmanagement/channel-management.js:36:30) at <anonymous> at process._tickDomainCallback (internal/process/next_tick.js:229:7)

我们需要向Google提出错误吗?

0 个答案:

没有答案