4
5

More than 3 years have passed since last update.

Node.jsでGoogle Drive上にフォルダ作成と存在確認 (Google Drive API v3)

Last updated at Posted at 2020-08-27

Google Drive上でフォルダを作成します。

メソッドはFiles: createになります。

ライブラリの書き方だとdrive.files.create()です。

スコープ指定

https://www.googleapis.com/auth/drive
https://www.googleapis.com/auth/drive.file
https://www.googleapis.com/auth/drive.appdata

Google Drive上で新規フォルダ作成

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

機能的にはfunction createFolderとかにリネームした方が良いでしょうが一旦動かす体なのでスルー

createFolder.js
//省略

async function listFiles(auth) {
  const drive = google.drive({version: 'v3', auth});
  const fileMetadata = {
    'name': 'n0bisuke', //作成したいフォルダの名前
    'mimeType': 'application/vnd.google-apps.folder'
  };
  const params = {
    resource: fileMetadata,
    fields: 'id'
  }
  try {
    const res = await drive.files.create(params);
    console.log(res.data);    
  } catch (error) {
    console.log(error);
  }
};

マイドライブのルートにn0bisukeというフォルダが作成されます。

フォルダを指定してフォルダ内にフォルダを新規作成

requestBodyにparents,name,mimeTypeを指定して実行します。


//省略
async function listFiles(auth) {
  const drive = google.drive({version: 'v3', auth});
  const params = {
    fields: 'id',
    requestBody: {
      name: 'n0bisuke', //新規作成するフォルダの名前
      mimeType: 'application/vnd.google-apps.folder'
      parents: [`xxxxxxxxxxxxxxxxxxxxxxx`], //新規作成したフォルダを置くフォルダのID
    }
  }
  try {
    const res = await drive.files.create(params);
    console.log(res.data);    
  } catch (error) {
    console.log(error);
  }
};

応用: 同名のフォルダが存在しなければフォルダを新規作成する

フォルダを自動的に作ったりするときに使いそうなのでメモしておきます。

  1. フォルダの確認
  2. 作成

という2ステップが考えられます。

1. 既に同じ名前のフォルダがあるかどうかの確認

Google Driveだと同じ名前のフォルダを同じ階層に作成することができてしまうので、防ぎたい場合は存在チェックを先にした方が良いと思います。(メソッドがなさそうな気がしたので手動)

FOLDER_IDで指定したフォルダの中を.list()で確認し、n0bisukeという名前のフォルダがあるか無いかをチェックします。

//省略

async function listFiles(auth) {

  const newFolderName = `n0bisuke`; //調べたいフォルダ名

  const drive = google.drive({version: 'v3', auth});
  const FOLDER_ID = `xxxxxxxxxxxxxxxx`; //ここにフォルダIDを指定
  const params = {
    q: `'${FOLDER_ID}' in parents and trashed = false`,
  }
  const res = await drive.files.list(params);
  const exists = res.data.files.find(file => file.name === newFolderName);

  if(exists) {
    console.log(`${newFolderName}は存在します。`);
  }else{
    console.log(`${newFolderName}は存在しません。`);
  }
};

2. フォルダの作成まで

まとめて書くとこんな感じです。

//省略

async function listFiles(auth) {
  const newFolderName = `n0bisuke`; //調べたいフォルダ名
  const FOLDER_ID = `xxxxxxxxxxxxxxxxxxxxxx`; //ここにフォルダIDを指定

  const drive = google.drive({version: 'v3', auth});
  const params = {
    q: `'${FOLDER_ID}' in parents and trashed = false`,
  }
  const res = await drive.files.list(params);
  const exists = res.data.files.find(file => file.name === newFolderName);

  if(exists) {
    console.log(`${newFolderName}は存在します。`);
  }else{
    console.log(`${newFolderName}は存在しません。`);
    console.log(`フォルダを新規作成します。`)
    const params = {
      fields: 'id',
      requestBody: {
        parents: [FOLDER_ID],
        name: newFolderName,
        mimeType: 'application/vnd.google-apps.folder'
      }
    }
    const res = await drive.files.create(params);
    console.log(res.data);    
  }
};

これで特定のフォルダ内にn0bisukeフォルダがあれば何もせず、n0bisukeフォルダが存在しなければ新規作成してくれます。

よもやま

.create()の時に重複を許可しない指定っぽいのができればこんなの周りくどいことしなくてよいのでもしそんな指定の仕方知ってる人いたら教えてください。

4
5
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
4
5