使用node.js从Google云端硬盘读取二进制文件 [英] Reading a binary file from Google Drive using node.js

查看:43
本文介绍了使用node.js从Google云端硬盘读取二进制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了使用API​​从云端硬盘获取二进制文件的问题,我一直盘旋.

I'm running into a problem getting a binary from Drive using the API, I keep going in circles.

以下是相关的代码位:

// Load client secrets from a local file.
fs.readFile('client_secret.json', function processClientSecrets(err, content) {
  if (err) {
    console.log('Error loading client secret file: ' + err);
    return;
  }
  // Authorize a client with the loaded credentials, then call the
  // Drive API.
  oauth.authorize(JSON.parse(content), dasm.init, driveapi.getFile)
});

driveapi.getFile:

driveapi.getFile:

function getFile(auth, cb) {
  var service = google.drive('v3');
  service.files.get({
    auth: auth,
    pageSize: 20,
    fileId: "0B2h-dgPh8_5CZE9WZVM4a3BxV00",
    alt: 'media'
  }, function(err, response) {
    if (err) {
      console.log('The API returned an error: ' + err);
      return;
    }
    cb(response)
  });
}

现在,响应似乎以字符串形式返回.当我尝试转换为十六进制时,它会发疯.有什么方法可以使响应进入 Buffer ?还是我从 service.files.get 获得它的那一秒损坏了?

Now, response appears to be coming back as a string. When I try to convert to hex it goes nuts. Is there any way to to take response and get it into a Buffer? Or is it corrupted the sec I get it from service.files.get ?

疯了,我是说

console.log(
        arrData[0].charCodeAt(0).toString(2),
        '-',
        arrData[1].charCodeAt(0).toString(2),
        '-',
        arrData[2].charCodeAt(0).toString(2),
        '-',
        arrData[3].charCodeAt(0).toString(2),
        '-',
        arrData[4].charCodeAt(0).toString(2)
    )

= 1001101-1011010-1111111111111101-0-11(我正在使用二进制文件尝试查看已损坏的内容)

= 1001101 - 1011010 - 1111111111111101 - 0 - 11 (I'm using binary to try to see what is broken)

正确的十六进制应为4D 5A 90 00 03

The correct hex would be 4D 5A 90 00 03

对于那些像我一样感到困惑的人来说, 90 是如何变成 fffd 的是

For those who are confused, like I was, how 90 became fffd it's the Unicode replacement character that gets displayed when the value doesn't map to an ASCII char.

推荐答案

终于可以解决这个问题.Google API使用请求模块,您可以应用任何选项它接受.作为参考,您需要设置 [encoding:null] 2 ,因为其他任何选项都会通过toString传递响应,因此如果您使用二进制数据,则会破坏响应.

Was able to solve this, finally. Google APIs use the request module, and you can apply any options that it accepts. For reference, you will need to set [encoding: null]2, as any other option will pass the response though toString, thus ruining it if you are working with binary data.

工作代码位于以下位置:

Working code is located below:

function getFile(auth, cb) {
  var service = google.drive({
    version: 'v3', 
    encoding: null
  });
  service.files.get({
    auth: auth,
    fileId: "0B2h-dgPh8_5CZE9WZVM4a3BxV00",
    alt: 'media'
  }, function(err, response) {
    if (err) {
      console.log('The API returned an error: ' + err);
      return;
    }
    cb(response)
  });
}

这篇关于使用node.js从Google云端硬盘读取二进制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆