在 NodeJS 中下载 Dropbox 文件的内容

Download contents of Dropbox File in NodeJS

我已经在 Dropbox API 应用程序控制台中设置了一个应用程序,对其进行了配置,设置了访问范围,并生成了一个访问令牌。

我让客户端 JS 使用 npm dropbox 包处理此代码。

<script>
  import Dropbox from 'dropbox';
  var ACCESS_TOKEN = 'MYACCESSTOKEN';
  var SHARED_LINK = 'https://www.dropbox.com/s/0-0-0-0/Link.txt';
  
  var dbx = new Dropbox.Dropbox({ accessToken: ACCESS_TOKEN });
  dbx
    .sharingGetSharedLinkFile({ url: SHARED_LINK })
    .then(function (data) {
      var blob = data.result.fileBlob;
      var reader = new FileReader();
      reader.addEventListener(
        'loadend',
        function () {
          console.log(reader.result);
        },
        { once: true }
      );
      reader.readAsText(blob);
    })
    .catch(function (error) {
      console.log(error);
    });
</script>

这会按照我的预期为我提供文件内容的控制台输出。现在,我正在尝试将其移动到我的节点后端。

var Dropbox = require('dropbox').Dropbox;
var fetch = require('isomorphic-fetch');
var ACCESS_TOKEN = 'MYACCESSTOKEN';

var dbx = new Dropbox({ accessToken: ACCESS_TOKEN, fetch: fetch });
dbx.filesDownload( {path: '/link.txt' })
  .then(function(response) {
    var blob = response.result
    console.log(blob)
    // var reader = new FileReader();
    // reader.addEventListener('loadend', function () {
    //   res.status(200).text(reader.result);
    //   console.log(reader.result);
    // }, {once: true});
    //reader.readAsText(blob);
  });
}

这输出

{ name: 'Link.txt',
  path_lower: '/link.txt',
  path_display: '/Link.txt',
  id: 'id:PpUnRfeu2cwAAsdAAAAD3PgA',
  client_modified: '2020-07-10T22:12:01Z',
  server_modified: '2020-08-01T15:22:11Z',
  rev: '015abd27832sdb8d900000001e1d2b470',
  size: 110,
  is_downloadable: true,
  content_hash:
   '1d46879a11f6dc9a720f5fd0sd79f53d6d4b1c7c6677523d55935e742bc0f921ab',
  fileBinary:
   <Buffer 68 74 74 70 73 3a 2f 2f 6c 69 76 65 74 72 61 63 6b 2e 67 61 72 6d 69 6e 2e 63 6f 6d 2f 73 65 73 73 69 6f 6e 2f 35 30 31 36 61 38 36 34 2d 64 39 37 64 ... > }

奇怪,这里没有 data.result.fileBlob,但我想我是在寻找 fileBinary。由于节点中没有 fileReader,我假设我必须使用 fs 或其他库来解码二进制文件,但我找不到示例。

我最终使用了一个包 blob-util,特别是 arrayBuffertoBinaryString

var Dropbox = require('dropbox').Dropbox;
var fetch = require('isomorphic-fetch');
import { arrayBufferToBinaryString } from 'blob-util'

  var ACCESS_TOKEN = 'MYACCESSTOKEN';
  var dbx = new Dropbox({ accessToken: ACCESS_TOKEN, fetch: fetch });
  dbx.filesDownload( {path: '/link.txt' })
    .then(function(response) {
      var blob = response.result.fileBinary
     console.log(arrayBufferToBinaryString(blob))
    })
  }
}

您可以使用节点 File System API 中的方法,而无需下载额外的包。

var Dropbox = require('dropbox').Dropbox;
var fetch = require('isomorphic-fetch');
var ACCESS_TOKEN = 'MYACCESSTOKEN';
var fs = require('fs');

dbx.filesDownload( {path: '/link.txt' })
  .then(function(response) {
    var fileName = response.name;
    fs.writeFile(`/your/path/${fileName}`, response.finalBinary, function (err, data) {
      if (err) throw err;
      console.log(data);
  });
}

fs.writeFile method 期待 filenamedata。在这种情况下,fileBinary 就是我们需要的数据。