Reading a binary file from Google Drive using node.js

I ran into the problem of getting binary code from disk using the API, I keep working in circles.

Here are the relevant bits of code:

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

 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) }); } 

Now response appears as a string. When I try to convert to hex, it goes crazy. Is there a way to take response and get it in Buffer ? Or is it damaged when I get it from service.files.get ?

By nuts, I mean that

 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 (I use binary code to try to see what is broken)

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

Edit: for those who are embarrassed, like me, 90 become the fffd displayable Unicode replacement character when the value is not displayed on an ASCII char.

+5
source share
2 answers

I was able to solve it finally. The Google API uses a request module, and you can apply any parameters that it accepts. For reference, you need to set [encoding: null] 2 , since any other option transmits a response, although toString, thereby destroying it if you are working with binary data.

The 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) }); } 
+3
source

This answer is based on an article in MDN about sending and receiving binary data

 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; } var arrayBuffer = response; if (arrayBuffer) { var byteArray = new Uint8Array(arrayBuffer); for (var i = 0; i < byteArray.byteLength; i++) { // do something with each byte in the array } } } 

If you do not get an array of bytes, you will have to convert the string to bytearray using the code below.

 var bytes = []; for (var i = 0, len = response.length; i < len; ++i) { bytes.push(str.charCodeAt(i)); } var byteArray = new Uint8Array(bytes); for (var i = 0; i < byteArray.byteLength; i++) { // do something with each byte in the array } 
0
source

Source: https://habr.com/ru/post/1247821/


All Articles