使用Google API的Google云端硬盘上传文件

使用Google API的Google云端硬盘上传文件

问题描述:

基于Google API的link。由于文件较大,我想使用分段上传。但是这有很多我不明白的东西。请先进。使用Google API的Google云端硬盘上传文件

基于谷歌API文档,要使用简单的上传

POST https://www.googleapis.com/upload/drive/v3/files?uploadType=media 

什么我尝试是如下,但实在不行

https://www.googleapis.com/upload/drive/v3/C:\Users\RNKP74\Desktop\Full_XML.zip?uploadType=media 

从谷歌REST链接,它显示的例子如下,如何运行?

POST /upload/drive/v3/files?uploadType=multipart HTTP/1.1 
Host: www.googleapis.com 
Authorization: Bearer your_auth_token 
Content-Type: multipart/related; boundary=foo_bar_baz 
Content-Length: number_of_bytes_in_entire_request_body 

--foo_bar_baz 
Content-Type: application/json; charset=UTF-8 

{ 
    "name": "My File" 
} 

--foo_bar_baz 
Content-Type: image/jpeg 

JPEG data 
--foo_bar_baz-- 
+0

任何人都可以告诉我如何上传文件到我的谷歌驱动器?我完成了所有的设置。 – Yeep

+0

查找POST与GET。 –

使用Files:insert此方法支持/上传URI并接受上传的媒体。 Official Google Documentation包含示例。

首先,将新文件元数据POST到驱动器端点。它是在一个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表示。它看起来像:

{ 
"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授权请求将实际文件的内容放入上载端点。它应该看起来像:

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> 

您也可以需要Resumable upload,它如果要传输大文件和网络中断的可能性或其他传输问题是高,例如,从上传时特别有用移动客户端应用。它还可以在发生网络故障时减少带宽使用量,因为您不必从头开始重新启动大型文件上传。