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

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

问题描述

需要关于通过API将文件插入谷歌驱动器的帮助。为此目的的api文档并没有清楚地解释如何通过http post请求发送文件的实际主体。

有关插入操作的文档已经包含了一些编程语言的示例,下面是如何操作使用Google Drive API的HTTP协议。

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

  POST / drive / v2 / files HTTP / 1.1 
主持人:www.googleapis.com
授权:持票人< OAuth 2.0在此访问令牌>
...

{
title:file_name.extension,
mimeType:mime / type,
description :关于文件的内容
}

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

  {
kind:drive#file,
id :string,
etag:etag,
selfLink:string,
title:file_name,
mimeType:mime / type,
description:关于文件的内容
...
downloadUrl:string,
...
}

这是确认已创建文件条目。现在您需要上传内容。为此,您需要在上述响应中获取由 id JSON属性给出的文件的ID,并使用OAuth 2.0授权请求将实际文件的内容放入上传端点。它应该如下所示:

  PUT / upload / drive / v2 / files / {id}?uploadType = media HTTP / 1.1 
主持人:www.googleapis.com
授权:承载人< OAuth 2.0访问令牌>
Content-Type:mime / type

<文件内容在这里>

您完成了:)

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

  POST / upload / drive / v2 / files HTTP / 1.1 
主持人:www.googleapis .com
授权:承载者< OAuth 2.0访问令牌>
Content-Type:multipart / form-data; border = 287032381131322
...

--287032381131322
Content-Type:application / json

{
title: file_name.extension,
mimeType:mime / type,
description:关于文件的内容
}
--287032381131322
Content -type:mime / type

<文件内容在这里>
--287032381131322--

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



最后还有一个可恢复上传协议,它是方便上传大文件,提供暂停/恢复功能和/或上传带有片状互联网连接的文件。



PS:现在大部分内容都在驱动器的文件上传文档


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.

解决方案

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.

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

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,
  ...
}

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>

You are done :)

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

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.

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: most of this is now described in Drive's file upload documentation.

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

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