1

I am trying to fetch data of all the files of a particular folder from google drive. For which I have done all the steps mentioned in https://developers.google.com/drive/api/v3/quickstart/nodejs

Also I am able to get the list of files of the folder based on the folder id in the drive. But I am not understanding how to get the actual data inside the files. With the below code I am getting the data in buffer format. How to convert this buffer format into actual text format and get the actual data.

Here is my code

    const fs = require('fs');
const readline = require('readline');
const {google} = require('googleapis');
const zlib = require("zlib");
const creds = require("../../../credentials.json")
const getfilelist = require("google-drive-getfilelist");
module.exports = async (req, res) => {
    try {
// If modifying these scopes, delete token.json.
const SCOPES = ['https://www.googleapis.com/auth/drive'];
// The file token.json stores the user's access and refresh tokens, and is
// created automatically when the authorization flow completes for the first
// time.
const TOKEN_PATH = 'token.json';

// Load client secrets from a local file.

  authorize(creds, listFiles);

/**
 * Create an OAuth2 client with the given credentials, and then execute the
 * given callback function.
 * @param {Object} credentials The authorization client credentials.
 * @param {function} callback The callback to call with the authorized client.
 */
function authorize(credentials, callback) {
  const oAuth2Client = new google.auth.OAuth2(
    credentials.client_id, credentials.client_secret, credentials.redirect_uris[0]);

  // Check if we have previously stored a token.
  fs.readFile(TOKEN_PATH, (err, token) => {
    if (err) return getAccessToken(oAuth2Client, callback);
    oAuth2Client.setCredentials(JSON.parse(token));
    callback(oAuth2Client);
  });
}

/**
 * Get and store new token after prompting for user authorization, and then
 * execute the given callback with the authorized OAuth2 client.
 * @param {google.auth.OAuth2} oAuth2Client The OAuth2 client to get token for.
 * @param {getEventsCallback} callback The callback for the authorized client.
 */
function getAccessToken(oAuth2Client, callback) {
  const authUrl = oAuth2Client.generateAuthUrl({
    access_type: 'offline',
    scope: SCOPES,
  });
  console.log('Authorize this app by visiting this url:', authUrl);
  const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
  });
  rl.question('Enter the code from that page here: ', (code) => {
    rl.close();
    oAuth2Client.getToken(code, (err, token) => {
      if (err) return console.error('Error retrieving access token', err);
      oAuth2Client.setCredentials(token);
      // Store the token to disk for later program executions
      fs.writeFile(TOKEN_PATH, JSON.stringify(token), (err) => {
        if (err) return console.error(err);
        console.log('Token stored to', TOKEN_PATH);
      });
      callback(oAuth2Client);
    });
  });
}

/**
 * Lists the names and IDs of up to 10 files.
 * @param {google.auth.OAuth2} auth An authorized OAuth2 client.
 */
function listFiles(auth) {
  const topFolderId = "1MTER3d8AVkpw0-_32xpxzZtyuP8iMH66"; // Please set the top folder ID.
getfilelist.GetFileList(
  {
    auth: auth,
    fields: "files(*)",
    id: topFolderId,
  },
  (err, res) => {
    if (err) {
      console.log(err);
      return;
    }
    const fileList = res.fileList.flatMap(({ files }) => files);
    fileList.map((file) => {
      console.log('iiiiii',file)
      downloadFile(auth, file.id)
    });
  }
);
}

function downloadFile(auth, fileId) {const drive = google.drive({version: 'v3', auth});
console.log('fileIdfileId',fileId)
    var dest = fs.createWriteStream("sample.txt");  // Please set the filename of the saved file.
drive.files.get(
  {fileId: fileId, alt: "media"},
  {responseType: "stream"},
  (err, {data}) => {
    if (err) {
      console.log(err);
      return;
    }
    data
      .on("end", () => console.log("Done."))
      .on("error", (err) => {
        console.log(err);
        return process.exit();
      })
      .pipe(dest);
  }
);




}
} catch (error) {
    console.log(error);
}
};

Near the code console.log('actual data',bdata) I am getting the output something like PK"♠�S↕word/numbering.xml��MN�0►�O�↔"��$§ ¶5� 6�♥���X�=��I��q��R$��U��������Kɠ�h♣��ˈ♦\3Ȅ.R���x"�uTgT��)9rK���u��J�9�}�Gh�(���9���e%W�.�p�9���/§�Ce▬♀��N�¶�↑���t↑HI�:�►♂%↑��ܝ$ ���� ���Jv�*ŵ;;�ȥ�☺�-��=Mͥ�b�C�♫Q+��k�¶�♀i��dk�f♠�qk��][∟�q4a�'Ġ���OϾ‼E�▲0�t\�♠���vF�↓ga�F�қ�#���.�y^ꍘ��+�W� PI‼C⌂h☺=♣PK"♠�S◄word/settings.xml���n�0♀ǟ�►���I6↑uzX�����=#ɶ►}A����'ǖդ@�f�H⌂�?�♀M?>�§|q��2%K��R��↕+�d]�?�?����:�♦���Dgj����cWX���♂O��►�D�s�H↕�����♦8⌂5u"�∟[��Jhp��8s�$O�-→1�D��ňX

and near the console.log('buffered data', bdata) I am getting the output as <Buffer 50 4b 03 04 14 00 08 08 08 00 22 06 ef bf bd 53 00 00 00 00 00 00 00 00 00 00 00 00 12 00 00 00 77 6f 72 64 2f 6e 75 6d 62 65 72 69 6e 67 2e 78 6d 6c ... 15880 more bytes>

How do I get the actual text from the file.

For your reference, this is the link of my sample text file https://drive.google.com/file/d/1gOug6I6oD7feINf9WLq4ba8E8pq_uZ7a/view

Can some one please help

Sai sri
  • 515
  • 12
  • 25
  • Your sample file of `https://drive.google.com/file/d/1gOug6I6oD7feINf9WLq4ba8E8pq_uZ7a/view` is `test file`. In the case of buffer, it's ``. When your sample shared file is used, `test file` can be seen. But, it seems that your sample data in your question is different from your sample shared file. Is that the DOCX or DOC file? If my understanding is correct when you tested your shared file using your script, what result did you obtain? – Tanaike Dec 09 '21 at 13:00
  • @Tanaike No it is the data of different file data. I just gave the link of file to make you understand what type of file I am referring to – Sai sri Dec 09 '21 at 15:26
  • Thank you for replying. I apologize that my comment was not useful. – Tanaike Dec 09 '21 at 23:13
  • @I hope this is helpful to you Yes I tried I tried to push into the file var dest = fs.createWriteStream("sample.txt");, but the data inside "sample.txt" is in some weird format. I have added the format my question and added the output of "sample.txt". How do I get the actual text format. – Sai sri Dec 10 '21 at 10:56
  • @Saisri As I mentioned in your [other thread](https://stackoverflow.com/q/70182931/10612011), remove the second parameter (`{responseType: "stream"}`) if you want to retrieve the text content. Your question got closed and I cannot post an answer, but just remove that (and remove the `data.on().on().pipe`, since you won't be using it). If you remove the second parameter, `data` will include your desired information. Use `console.log(data)` after the `if` block to check that's the case. – Iamblichus Dec 13 '21 at 09:19

0 Answers0