google-drive-getfilelist

1.0.5 • Public • Published

node-getfilelist

Build Status MIT License

Overview

This is a Node.js module to retrieve the file list with the folder tree from the specific folder of own Google Drive and shared Drives.

Description

When I create applications for using Google Drive, I often retrieve a file list from a folder in the application. So far, I had created the script for retrieving a file list from a folder for each application. Recently, I thought that if there is the script for retrieving the file list with the folder tree from the folder of Google Drive as a module, it will be useful for me and other users. So I created this.

Features

  • This module retrieves all files from a folder in Google Drive.
  • All files include the folder structure in Google Drive.
  • Only folder tree can be also retrieved.

Install

$ npm install --save-dev google-drive-getfilelist

or

$ npm install --global google-drive-getfilelist

You can also see this module at https://www.npmjs.com/package/google-drive-getfilelist.

Method

Method Explanation
GetFolderTree(object) Retrieve only folder structure from a folder
GetFileList(object) Retrieve file list with folder structure from a folder

Usage

There are 3 patterns for using this library.

1. Use API key

This is a sample script using API key. When you want to retrieve the API key, please do the following flow.

  1. Login to Google.
  2. Access to https://console.cloud.google.com/?hl=en.
  3. Click select project at the right side of "Google Cloud Platform" of upper left of window.
  4. Click "NEW PROJECT"
    1. Input "Project Name".
    2. Click "CREATE".
    3. Open the created project.
    4. Click "Enable APIs and get credentials like keys".
    5. Click "Library" at left side.
    6. Input "Drive API" in "Search for APIs & Services".
    7. Click "Google Drive API".
    8. Click "ENABLE".
    9. Back to https://console.cloud.google.com/?hl=en.
    10. Click "Enable APIs and get credentials like keys".
    11. Click "Credentials" at left side.
    12. Click "Create credentials" and select API key.
    13. Copy the API key. You can use this API key.

Sample script

const getfilelist = require("google-drive-getfilelist");
 
const resource = {
  auth: "### API key ###",
  id: "### Folder ID ###",
  fields: "files(name,id)",
};
 
getfilelist.GetFileList(resource, function (err, res) {
  // or getfilelist.GetFolderTree(resource, function(err, res) {
  if (err) {
    console.log(err);
    return;
  }
  console.log(res);
});

Note

  • When you want to retrieve the file list from the folder using API key, the folder is required to be shared.
  • You can modify the property of fields. When this is not used, the default fields are used.

2. Use OAuth2

Document of OAuth2 is here.

Sample script

In this sample script, the process of OAuth2 uses the quickstart of Google. Please check this.

const fs = require("fs");
const readline = require("readline");
const { google } = require("googleapis");
 
// If modifying these scopes, delete token.json.
const SCOPES = ["https://www.googleapis.com/auth/drive.metadata.readonly"];
// 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.
fs.readFile("credentials.json", (err, content) => {
  if (err) return console.log("Error loading client secret file:", err);
  // Authorize a client with credentials, then call the Google Drive API.
  authorize(JSON.parse(content), getfilelist);
});
 
/**
 * 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 { client_secret, client_id, redirect_uris } = credentials.installed;
  const oAuth2Client = new google.auth.OAuth2(
    client_id,
    client_secret,
    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) console.error(err);
        console.log("Token stored to", TOKEN_PATH);
      });
      callback(oAuth2Client);
    });
  });
}
 
/**
 * Using getfilelist.
 */
function getfilelist(auth) {
  const getfilelist = require("google-drive-getfilelist");
 
  const resource = {
    auth: auth,
    id: "### Folder ID ###",
    fields: "files(name,id)",
  };
 
  getfilelist.GetFileList(resource, function (err, res) {
    // or getfilelist.GetFolderTree(resource, function(err, res) {
    if (err) {
      console.log(err);
      return;
    }
    console.log(res);
  });
}

Note

  • Here, as a sample, the script of the authorization uses the script of quickstart.
  • You can modify the property of fields. When this is not used, the default fields are used.

3. Use Service account

Document of Service account is here.

Sample script

const { google } = require("googleapis");
const getfilelist = require("google-drive-getfilelist");
const key = require("### json file including public/private key pair ###");
 
const jwtClient = new google.auth.JWT(
  key.client_email,
  null,
  key.private_key,
  ["https://www.googleapis.com/auth/drive"],
  null
);
 
const resource = {
  auth: jwtClient,
  id: "### Folder ID ###",
  fields: "files(name,id)",
};
 
getfilelist.GetFileList(resource, function (err, res) {
  // or getfilelist.GetFolderTree(resource, function(err, res) {
  if (err) {
    console.log(err);
    return;
  }
  console.log(res);
});

Note

  • You can modify the property of fields. When this is not used, the default fields are used.

Values

As a sample, when the values are retrieved from above structure, the results of GetFolderTree() becomes as follows.

Values retrieved by GetFolderTree()

getfilelist.GetFolderTree(resource, function (err, res) {
  if (err) {
    console.log(err);
    return;
  }
  console.log(res);
});
{
  "id": [
    ["folderIdOfsampleFolder1"],
    ["folderIdOfsampleFolder1", "folderIdOfsampleFolder_2a"],
    ["folderIdOfsampleFolder1", "folderIdOfsampleFolder_2b"],
    [
      "folderIdOfsampleFolder1",
      "folderIdOfsampleFolder_2a",
      "folderIdOfsampleFolder_2a_3a"
    ],
    [
      "folderIdOfsampleFolder1",
      "folderIdOfsampleFolder_2b",
      "folderIdOfsampleFolder_2b_3a"
    ],
    [
      "folderIdOfsampleFolder1",
      "folderIdOfsampleFolder_2b",
      "folderIdOfsampleFolder_2b_3b"
    ],
    [
      "folderIdOfsampleFolder1",
      "folderIdOfsampleFolder_2b",
      "folderIdOfsampleFolder_2b_3b",
      "folderIdOfsampleFolder_2b_3b_4a"
    ]
  ],
  "names": [
    "sampleFolder1",
    "sampleFolder_2a",
    "sampleFolder_2b",
    "sampleFolder_2a_3a",
    "sampleFolder_2b_3a",
    "sampleFolder_2b_3b",
    "sampleFolder_2b_3b_4a"
  ],
  "folders": [
    "folderIdOfsampleFolder1",
    "folderIdOfsampleFolder_2a",
    "folderIdOfsampleFolder_2b",
    "folderIdOfsampleFolder_2a_3a",
    "folderIdOfsampleFolder_2b_3a",
    "folderIdOfsampleFolder_2b_3b",
    "folderIdOfsampleFolder_2b_3b_4a"
  ]
}

Values retrieved by Do()

getfilelist.GetFileList(resource, function (err, res) {
  if (err) {
    console.log(err);
    return;
  }
  console.log(res);
});
{
  "searchedFolder": {
    "id": "###",
    "name": "sampleFolder1",
    "mimeType": "application/vnd.google-apps.folder",
    "parents": ["###"],
    "createdTime": "2000-01-01T01:23:45.000Z",
    "modifiedTime": "2000-01-01T01:23:45.000Z",
    "webViewLink": "https://drive.google.com/drive/folders/###",
    "owners": [
      { "displayName": "###", "permissionId": "###", "emailAddress": "###" }
    ],
    "shared": true
  },
  "folderTree": {
    "id": [
      ["folderIdOfsampleFolder1"],
      ["folderIdOfsampleFolder1", "folderIdOfsampleFolder_2a"],
      ["folderIdOfsampleFolder1", "folderIdOfsampleFolder_2b"],
      [
        "folderIdOfsampleFolder1",
        "folderIdOfsampleFolder_2a",
        "folderIdOfsampleFolder_2a_3a"
      ],
      [
        "folderIdOfsampleFolder1",
        "folderIdOfsampleFolder_2b",
        "folderIdOfsampleFolder_2b_3a"
      ],
      [
        "folderIdOfsampleFolder1",
        "folderIdOfsampleFolder_2b",
        "folderIdOfsampleFolder_2b_3b"
      ],
      [
        "folderIdOfsampleFolder1",
        "folderIdOfsampleFolder_2b",
        "folderIdOfsampleFolder_2b_3b",
        "folderIdOfsampleFolder_2b_3b_4a"
      ]
    ],
    "names": [
      "sampleFolder1",
      "sampleFolder_2a",
      "sampleFolder_2b",
      "sampleFolder_2a_3a",
      "sampleFolder_2b_3a",
      "sampleFolder_2b_3b",
      "sampleFolder_2b_3b_4a"
    ],
    "folders": [
      "folderIdOfsampleFolder1",
      "folderIdOfsampleFolder_2a",
      "folderIdOfsampleFolder_2b",
      "folderIdOfsampleFolder_2a_3a",
      "folderIdOfsampleFolder_2b_3a",
      "folderIdOfsampleFolder_2b_3b",
      "folderIdOfsampleFolder_2b_3b_4a"
    ]
  },
  "fileList": [
    {
      "folderTree": ["folderIdOfsampleFolder1"],
      "files": [
        {
          "name": "Spreadsheet1",
          "mimeType": "application/vnd.google-apps.spreadsheet"
        }
      ]
    },
    {
      "folderTree": ["folderIdOfsampleFolder1", "folderIdOfsampleFolder_2a"],
      "files": [
        {
          "name": "Spreadsheet2",
          "mimeType": "application/vnd.google-apps.spreadsheet"
        }
      ]
    },
    {
      "folderTree": ["folderIdOfsampleFolder1", "folderIdOfsampleFolder_2b"],
      "files": [
        {
          "name": "Spreadsheet4",
          "mimeType": "application/vnd.google-apps.spreadsheet"
        }
      ]
    },
    {
      "folderTree": [
        "folderIdOfsampleFolder1",
        "folderIdOfsampleFolder_2a",
        "folderIdOfsampleFolder_2a_3a"
      ],
      "files": null
    },
    {
      "folderTree": [
        "folderIdOfsampleFolder1",
        "folderIdOfsampleFolder_2b",
        "folderIdOfsampleFolder_2b_3a"
      ],
      "files": [
        {
          "name": "Spreadsheet3",
          "mimeType": "application/vnd.google-apps.spreadsheet"
        }
      ]
    },
    {
      "folderTree": [
        "folderIdOfsampleFolder1",
        "folderIdOfsampleFolder_2b",
        "folderIdOfsampleFolder_2b_3b"
      ],
      "files": null
    },
    {
      "folderTree": [
        "folderIdOfsampleFolder1",
        "folderIdOfsampleFolder_2b",
        "folderIdOfsampleFolder_2b_3b",
        "folderIdOfsampleFolder_2b_3b_4a"
      ],
      "files": [
        {
          "name": "Document1",
          "mimeType": "application/vnd.google-apps.document"
        },
        {
          "name": "image1.png",
          "mimeType": "image/png"
        },
        {
          "name": "Slides1",
          "mimeType": "application/vnd.google-apps.presentation"
        },
        {
          "name": "Spreadsheet5",
          "mimeType": "application/vnd.google-apps.spreadsheet"
        },
        {
          "name": "StandaloneProject1",
          "mimeType": "application/vnd.google-apps.script"
        },
        {
          "name": "Test1.txt",
          "mimeType": "text/plain"
        }
      ]
    }
  ],
  "totalNumberOfFiles": 10,
  "totalNumberOfFolders": 7
}

For other languages

As the libraries "GetFileList" for other languages, there are following libraries.


Licence

MIT

Author

Tanaike

If you have any questions and commissions for me, feel free to tell me.

Update History

  • v1.0.0 (November 14, 2018)

    1. Initial release.
  • v1.0.3 (November 16, 2018)

    1. Modified a bug.
  • v1.0.4 (May 14, 2020)

    1. Shared drive got to be able to be used. The file list can be retrieved from both your Google Drive and the shared drive.

      • For example, when the folder ID in the shared Drive is used id of resource, you can retrieve the file list from the folder in the shared Drive.
  • v1.0.5 (June 1, 2020)

    1. When the file is retrieved from the shared drive, the parameter was not completed. This bug was removed.

TOP

Dependencies (0)

    Dev Dependencies (1)

    Package Sidebar

    Install

    npm i google-drive-getfilelist

    Weekly Downloads

    51

    Version

    1.0.5

    License

    MIT

    Unpacked Size

    54.5 kB

    Total Files

    6

    Last publish

    Collaborators

    • tanaike