傳回檔案資源中的標籤

本頁面說明如何從 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 方法。

附註