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

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

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

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

例

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

Java

File file = driveService.files()
    .get("FILE_ID")s
    .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 メソッドを使用します。

メモ

  • ファイル リソースを返すメソッドは、fieldincludeLabelsとクエリ パラメータをサポートしています。たとえば、 files.copy、 files.list、および files.update などです。