ファイル リソースからラベルを返す

このページでは、Google ドライブのファイル リソースから特定のラベルを返す方法について説明します。

取得するラベルを指定するには、files.get メソッドか、ファイル リソースを返す任意のメソッドを使用します。リクエストの本文は空にする必要があります。

成功した場合、レスポンスの本文には File のインスタンスが含まれます。

次のコードサンプルは、fileIdlabelId を使用して、特定のラベルのセットを返す方法を示しています。includeLabels オブジェクトは、ID のカンマ区切りのリストです。fields パラメータの labelInfo オブジェクトには、ファイルに設定され、includeLabels 内でリクエストされたラベルが含まれています。

Java

File file = driveService.files().get("FILE_ID").setIncludeLabels("LABEL_ID,LABEL_ID").setFields("labelInfo").execute();

Python

file = drive_service.files().get(fileId="FILE_ID", includeLabels="LABEL_ID,LABEL_ID", fields="labelInfo").execute();

Node.js

/**
* Get a Drive file with specific labels
* @return{obj} file with labelInfo
**/
async function getFileWithSpecificLabels() {
  // Get credentials and build service
  // TODO (developer) - Use appropriate auth mechanism for your app

  const {GoogleAuth} = require('google-auth-library');
  const {google} = require('googleapis');

  const auth = new GoogleAuth({scopes: 'https://www.googleapis.com/auth/drive'});
  const service = google.drive({version: 'v3', auth});
  try {
    const file = await service.files.get({
      fileId: 'FILE_ID',
      includeLabels: 'LABEL_ID,LABEL_ID',
      fields:'labelInfo',
    });
    return file;
  } catch (err) {
    // TODO (developer) - Handle error
    throw err;
  }
}

次のように置き換えます。

  • FILE_ID: ラベルを含むファイルの fileId
  • LABEL_ID: 返されるラベルの labelId。ファイル内のラベルを見つけるには、files.listLabels メソッドを使用します。

メモ