使用 NodeJS 下载 Dropbox 文件内容

3
我已在Dropbox API应用控制台中设置了一个应用程序,对其进行了配置、设置了访问范围并生成了访问令牌。
我已使用npm dropbox包,通过客户端JS使其工作。

<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>

这让我在控制台输出了我想要的文件内容。现在,我正在尝试将其移动到我的Node后端。

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 ... > }

很好奇,这里没有数据.result.fileBlob,但我认为我在fileBinary之后。由于node中没有fileReader,我认为我必须使用fs或其他库来解码二进制文件,但我找不到示例。


fileBinary 属性是一个缓冲区,你可以直接将其写入文件。 - Vinay
这基本上是我的问题。我如何将fileBinary(文本文件数据)写入变量,以便我可以读取内容? - cycle4passion
2个回答

3

我最终使用了一个包blob-util,具体来说是用于将ArrayBuffer转换为二进制字符串的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))
    })
  }
}


3

你可以使用来自Node 文件系统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 方法 需要传入一个 filenamedata。在这种情况下,fileBinary 就是我们需要的数据。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接