本指南介绍了如何使用 Google Drive API 在 Google 云端硬盘中创建和管理文件。
创建文件
如需在云端硬盘中创建包含内容(媒体)的文件,您必须上传文件数据。您可以在单个请求中同时上传文件的元数据和内容(分段上传),也可以仅上传媒体(简单上传)。如需了解更多 信息,请参阅上传文件数据。
如需创建不包含元数据或内容的文件,请对 files 资源使用 create 方法,且不使用任何参数。
创建文件时,该方法会返回 files 资源。系统会为该文件指定
kind 的 drive.file、id、name“未命名”以及
mimeType 的 application/octet-stream。The
uploadType
会标记为必需,但默认值为 media,因此您实际上不必
提供它。
如需详细了解云端硬盘文件限制,请参阅文件和 文件夹限制。
以下代码示例展示了如何创建不包含元数据或内容的文件:
Node.js
/**
* Create an empty file.
* @return {string} The created file's ID.
*/
async function createEmptyFile() {
// 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: