Trang này mô tả cách trả về các nhãn cụ thể từ một tài nguyên tệp trên Google Drive.
Để chỉ định nhãn mà bạn muốn truy xuất, hãy sử dụng phương thức files.get
hoặc bất kỳ phương thức nào trả về tài nguyên tệp. Nội dung yêu cầu phải trống.
Nếu thành công, nội dung phản hồi sẽ chứa một bản sao của File
.
Ví dụ:
Mã mẫu sau đây cho biết cách sử dụng fileId
và labelId
để trả về tập hợp nhãn cụ thể. Đối tượng includeLabels
là một danh sách các mã được phân tách bằng dấu phẩy. Đối tượng labelInfo
trong tham số fields
chứa các nhãn được đặt trên tệp và được yêu cầu trong 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;
}
}
Thay thế đoạn mã sau:
- FILE_ID:
fileId
của tệp chứa nhãn. - LABEL_ID:
labelId
của một nhãn cần trả về. Để tìm nhãn trên một tệp, hãy sử dụng phương thứcfiles.listLabels
.
Ghi chú
- Mọi phương thức trả về tài nguyên tệp đều hỗ trợ trường
includeLabels
và tham số truy vấn. Ví dụ:files.copy
,files.list
vàfiles.update
.