返回文件资源中的标签
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
本页介绍了如何从 Google 云端硬盘文件资源中返回特定标签。
如需指定要检索的标签,请使用 files.get
方法或返回文件资源的任何方法。请求正文必须为空。
如果成功,响应正文将包含一个 File
实例。
示例
以下代码示例展示了如何使用 fileId
以及 labelId
返回一组特定标签。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
方法。
备注
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-29。
[null,null,["最后更新时间 (UTC):2025-08-29。"],[],[],null,["# Return a label from a file resource\n\nThis page describes how to return specific labels from a Google Drive file\nresource.\n\nTo specify which labels that you want to retrieve, use the\n[`files.get`](/workspace/drive/api/v2/reference/files/get) method or any method that\nreturns a [file resource](/workspace/drive/labels/reference/rest/v2/labels). The request\nbody must be empty.\n\nIf successful, the [response\nbody](/workspace/drive/api/reference/rest/v2/files/get#response-body) contains an instance\nof [`File`](/workspace/drive/api/reference/rest/v2/files#File).\n\nExample\n-------\n\nThe following code sample shows how to use the `fileId`, plus the `labelId`, to\nreturn the set of specific labels. The\n[`includeLabels`](/workspace/drive/api/reference/rest/v2/files/get#query-parameters)\nobject is a comma-separated list of IDs. The `labelInfo`object in the `fields`\nparameter contains labels set on the file and requested within `includeLabels`. \n\n### Java\n\n File file = driveService.files().get(\"\u003cvar translate=\"no\"\u003eFILE_ID\u003c/var\u003e\").setIncludeLabels(\"\u003cvar translate=\"no\"\u003eLABEL_ID\u003c/var\u003e,\u003cvar translate=\"no\"\u003eLABEL_ID\u003c/var\u003e\").setFields(\"labelInfo\").execute();\n\n### Python\n\n file = drive_service.files().get(fileId=\"\u003cvar translate=\"no\"\u003eFILE_ID\u003c/var\u003e\", includeLabels=\"\u003cvar translate=\"no\"\u003eLABEL_ID\u003c/var\u003e,\u003cvar translate=\"no\"\u003eLABEL_ID\u003c/var\u003e\", fields=\"labelInfo\").execute();\n\n### Node.js\n\n /**\n * Get a Drive file with specific labels\n * @return{obj} file with labelInfo\n **/\n async function getFileWithSpecificLabels() {\n // Get credentials and build service\n // TODO (developer) - Use appropriate auth mechanism for your app\n\n const {GoogleAuth} = require('google-auth-library');\n const {google} = require('googleapis');\n\n const auth = new GoogleAuth({scopes: 'https://www.googleapis.com/auth/drive'});\n const service = google.drive({version: 'v3', auth});\n try {\n const file = await service.files.get({\n fileId: '\u003cvar translate=\"no\"\u003eFILE_ID\u003c/var\u003e',\n includeLabels: '\u003cvar translate=\"no\"\u003eLABEL_ID\u003c/var\u003e,\u003cvar translate=\"no\"\u003eLABEL_ID\u003c/var\u003e',\n fields:'labelInfo',\n });\n return file;\n } catch (err) {\n // TODO (developer) - Handle error\n throw err;\n }\n }\n\nReplace the following:\n\n- \u003cvar translate=\"no\"\u003eFILE_ID\u003c/var\u003e: The `fileId` of the file containing the labels.\n- \u003cvar translate=\"no\"\u003eLABEL_ID\u003c/var\u003e: The `labelId` of a label to return. To locate the labels on a file, use the [`files.listLabels`](/workspace/drive/api/v2/reference/files/listLabels) method.\n\nNotes\n-----\n\n- Any method returning a [file\n resource](/workspace/drive/labels/reference/rest/v2/labels) supports the `includeLabels` field and query parameter. For example, [`files.copy`](/workspace/drive/api/reference/rest/v2/files/copy), [`files.list`](/workspace/drive/api/reference/rest/v2/files/list), and [`files.update`](/workspace/drive/api/reference/rest/v2/files/update)."]]