Return a label from a file resource

This page describes how to return labels from a Google Drive file resource. To specify which labels you want to retrieve, use the files.get method or any method which returns a file resource.

You also need to specify:

  • The fileId of the file for which the labels are modified.

  • includeLabels as a comma-separated list of labelIds.

  • The labelId of the label to return.

  • labelInfo to denote where in the response the labels for the file resource should be written.

This example uses the fileId plus the labelId to return the set of specific labels.

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");

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;
    }
  }

Notes

  • Labels accessible by a customer can be retrieved using the Drive Labels API.
  • The labelInfo object only contains labels set on the file and requested by the user in includeLabels.
  • Any endpoint returning a file resource supports the includeLabels field and query parameter. For example, files.list, files.copy, and files.update.
  • To list all labels, use the files.listLabels method.