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