I have setup up an App in the Dropbox API app console, configured it, set access scope, and generated an access token.
I have client side JS working with this code using the npm dropbox package.
<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>
and this gives me console output of the contents of my file as I intended. Now, I am trying to move it to the my node backend.
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);
});
}
This outputs
{ 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 ... > }
Curious, no data.result.fileBlob here, but I assume I am after fileBinary. Since there is no fileReader in node, I assume I have to use fs, or another library to decode the binary, but I am at a loss to find examples.
I ended up using a package blob-util, specifically the 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))
})
}
}
You can use methods from the Node File System API without downloading additional packages.
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);
});
}
The fs.writeFile method is expecting a filename and data. In this case, the fileBinary is the data we need.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With