如何通过 API 向 Google Drive 添加/创建/插入文件? [英] How do I add/create/insert files to Google Drive through the API?

查看:29
本文介绍了如何通过 API 向 Google Drive 添加/创建/插入文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要关于通过 api 将文件插入谷歌驱动器的帮助.用于此目的的 api 文档没有明确说明如何通过 http post 请求发送文件的实际正文.

Need help regarding inserting a file to google drive through api. The api documentation for this purpose does not clearly explains how to send the actual body of the file through http post request.

推荐答案

插入操作文档 已经包含了大量编程语言的示例,这里是如何使用 Google Drive API 的基于 HTTP 的协议来做到这一点.

The documentation on insert operations already contains examples in a bunch of programming languages, here is how to do it using the HTTP based protocol of the Google Drive API.

首先,将新文件元数据发布到 Drive 端点.它必须采用 文件资源 JSON 对象的形式:

First, POST the new file metadata to the Drive endpoint. It has to be in the form of a File resource JSON object:

POST /drive/v2/files HTTP/1.1
Host: www.googleapis.com
Authorization: Bearer <OAuth 2.0 access token here>
...

{
  "title": "file_name.extension",
  "mimeType": "mime/type",
  "description": "Stuff about the file"
}

响应正文将是新创建的文件资源的 JSON 表示.它看起来像:

The response body will be a JSON representation of the newly created File resource. It will look like:

{
  "kind": "drive#file",
  "id": string,
  "etag": etag,
  "selfLink": string,
  "title": "file_name",
  "mimeType": "mime/type",
  "description": "Stuff about the file"
  ...
  "downloadUrl": string,
  ...
}

这是确认文件条目已创建.现在您需要上传内容.为此,您需要获取上述响应中 id JSON 属性给出的文件 ID,并使用 OAuth 2.0 授权请求将实际文件的内容发送到上传端点.它应该看起来像:

This is a confirmation that the file entry has been created. Now you need to upload the content. To do that you need to take the ID of the file given by the id JSON attribute in the response above and PUT the content of the actual file to the upload endpoint with an OAuth 2.0 authorized request. It should look like:

PUT /upload/drive/v2/files/{id}?uploadType=media HTTP/1.1
Host: www.googleapis.com
Authorization: Bearer <OAuth 2.0 access token here>
Content-Type: mime/type

<file content here>

你完成了:)

还有一种方法可以使用多部分请求在 1 个单个 POST 请求中执行此操作,在该请求中您可以在发布内容的同时发布文件的元数据.下面是一个例子:

There is also a way to do this in 1 single POST request using a multipart request where you post the metadata of the file at the same time as the content. Here is an example:

POST /upload/drive/v2/files HTTP/1.1
Host: www.googleapis.com
Authorization: Bearer <OAuth 2.0 access token here>
Content-Type: multipart/form-data; boundary=287032381131322
...

--287032381131322
Content-Type: application/json

{
  "title": "file_name.extension",
  "mimeType": "mime/type",
  "description": "Stuff about the file"
}
--287032381131322
Content-Type: mime/type

<file content here>
--287032381131322--

响应将包含新创建文件的元数据.您还可以在请求的子部分使用 Content-Transfer-Encoding: base64 标头,以便能够以 Base 64 编码的形式传递文件数据.

The response will contain the metadata of the newly created file. You may also use the Content-Transfer-Encoding: base64 header in the sub-part of the request to be able to pass the data of the file as Base 64 encoded.

最后还有一个可恢复上传协议,方便上传大文件,提供暂停/resume 功能和/或使用不稳定的互联网连接上传文件.

Lastly there is also a resumable upload protocol which is convenient to upload large files, offer a pause/resume feature and/or upload files with flaky internet connection.

PS:大部分内容现在在云端硬盘的文件上传文档中进行了描述.

PS: most of this is now described in Drive's file upload documentation.

这篇关于如何通过 API 向 Google Drive 添加/创建/插入文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆