LoginSignup
12
10

More than 3 years have passed since last update.

Node.jsでGoogle Driveの指定フォルダからファイル一覧を取得メモ (Google Drive API v3)

Last updated at Posted at 2019-06-26

フォルダーIDを指定しての実行がなかなか見つからなかったのでメモです。

環境

  • Node.js v12.2.0
  • Google Drive API v3 (過去の調べるとv2も出てくるので念のため)

基本

公式のNode.jsチュートリアルをやります。

https://developers.google.com/drive/api/v3/quickstart/nodejs

フォルダIDを指定して取得

ブラウザで開いた時のURLのこの部分です。

スクリーンショット 2019-06-26 19.45.46.png

チュートリアルのfunction listFilesの箇所を書き換えます。

app.js

async function listFiles(auth) {
  const drive = google.drive({version: 'v3', auth});
  const FOLDER_ID = `xxxxxxxxxxxxxxxxxxxxxxxxxxxxx`; //ここにフォルダIDを指定
  const params = {
    q: `'${FOLDER_ID}' in parents and trashed = false`,
  }

  try {
    const res = await drive.files.list(params);
    const files = res.data.files;
    if (files.length) {
      console.log('Files:');
      files.map((file) => {
        console.log(`${file.name} (${file.id})`);
      });
    } else {
      console.log('No files found.');
    }    
  } catch (err) {
    console.log('The API returned an error: ' + err);
  }
}

所感

q'${FOLDER_ID}' in parents and trashed = falseなんて指定の仕方をするんですね.....

一応中のこの辺をみるとパラメータで何があるか分かりますが、公式のlist.jsってサンプルもparamsの書き方が書いてなくて見つけるのがしんどかった苦笑

driveIdとかteamDriveIdとか色々あるけど別物らしい(調べきれてない)

参考

12
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
10