如何使用python将断点续传文件上传到Google云端硬盘 [英] How to perform resumable file upload to Google Drive with python

查看:206
本文介绍了如何使用python将断点续传文件上传到Google云端硬盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将大文件(大于5 MB)上传到Google云端硬盘.根据Google的文档,我需要设置一个可恢复的上传会话.如果会话成功启动,您将收到带有会话URI的响应.然后将另一个我认为是您的文件的请求发送到URI.

I am trying to upload large files (greater than 5 MB) to Google Drive. Based Google's documentation, I would need to setup a resumable upload session. If the session is started successfully, you will get a response with a session URI. Then send another request to the URI with what I presume is your file.

我已经成功建立了断点续传会话,但是我不清楚您在哪里指定使用此方法上传文件的位置.请在下面查看我的代码.

I have been able to successfully set up the resumable session but I am unclear where you specify the location of your file for uploading with this method. Please see my code below.

Google要启动断点续传的方式

POST https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable HTTP/1.1
Authorization: Bearer [YOUR_AUTH_TOKEN]
Content-Length: 38
Content-Type: application/json; charset=UTF-8
X-Upload-Content-Type: application/octet-stream

{
  "name": "myObject"
}

我是如何在Python中完成的

import requests
from oauth2client.service_account import ServiceAccountCredentials

credentials = ServiceAccountCredentials.from_json_keyfile_dict(
    keyfile_dict=[SERVICE_ACCOUNT_JSON],
    scopes='https://www.googleapis.com/auth/drive')

delegated_credentials = credentials.create_delegated([EMAIL_ADDRESS])

access_token = delegated_credentials.get_access_token().access_token

url = "https://www.googleapis.com/upload/drive/v3/files"

querystring = {"uploadType": "resumable"}

payload = '{"name": "myObject", "parents": "[PARENT_FOLDER]"}'
headers = {
    'Content-Length': "38",
    'Content-Type': "application/json",
    'X-Upload-Content-Type': "application/octet-stream",
    'Authorization': "Bearer " + access_token
    }

response = requests.request(
    "POST", url, data=payload, headers=headers, params=querystring)

print(response.headers['Location'])

成功的响应位置URI

https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable&upload_id=[SOME_LONG_ID]

PUT请求Google想要

PUT https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable&upload_id=[SOME_LONG_ID] HTTP/1.1
Content-Length: 2000000
Content-Type: application/octet-stream

[BYTES 0-1999999]

在python中进行PUT请求-这是我开始迷路的地方

uri = response.headers['Location']

headers = {
    'Content-Length': "2000000",
    'Content-Type': "application/json"
    }

response = requests.request(
    "PUT", uri, headers=headers)

我想知道如何使用我的文件的路径以及所需的任何其他信息来完成此PUT请求.感谢您的帮助.

I would like to know how to complete this PUT request with the path to my file and any other information that is needed. Thanks for the help.

推荐答案

您几乎已经完成了,只有几件事:

You almost have it done, just a couple of things:

关于您在其中启动可恢复上传并发送元数据的第一个请求的有效负载:

Regarding the payload of the first request where you initiate the resumable upload and send the metadata:

payload = '{"name": "myObject", "parents": "[PARENT_FOLDER]"}'

您应该使用这种方式将文件存储在所选文件夹中:

You should put it this way to store the file in the selected folder:

payload = '{"name": "myObject2", "parents": ["PARENT_FOLDER_ID"]}'

唯一的更改是在每个父文件夹ID的方括号内使用引号("),这是因为API期望为Parent字段使用字符串数组(每个父文件夹ID的每个字符串)[1].

The only change would be using the quotation marks ("") inside the brackets for each parent folder id, this is because the API is expecting an array of strings for the parents field (each string for each parent folder id) [1].

对于可恢复上传的第二部分(上传文件),您只需要获取要上传的文件,并将其作为带有请求中"data"参数的请求正文发送,如下所示:

For the second part of the resumable upload (uploading the file), you just have to obtain the file you want to upload and send it as the request body with the "data" parameter in the request like this:

  uri = response.headers['Location']

    headers = {
        'Content-Length': "2000000",
        'Content-Type': "image/jpeg"  
    }

    #Open the file and stored it in data2
    in_file = open("filepath to the file", "rb")  # opening for [r]eading as [b]inary
    data2 = in_file.read()

    #Send the file in the request
    response = requests.request(
        "PUT", uri, data=data2, headers=headers)

使用函数open()[2],其文件路径包括文件名(相对或绝对),并使用"rb"作为第二个参数以二进制模式读取文件,您将获得原始二进制文件(文件对象)并对其应用read()[3]函数,您将获得二进制数据,该数据就是请求在请求正文中所期望的(数据参数).

Using the function open() [2] with the file path including the filename (relative or absolute) and using "rb" as the 2nd parameter to read the file in binary mode, you will get a raw binary file (file object) and applying the read() [3] function to it you will get the binary data, which is what the request is expecting in the request body (data parameter).

我测试了上面提供的代码,将图像上传到特定的文件夹,并且可以正常工作.记住要更改内容类型.

I tested the code provided above uploading an image to a specific folder and it worked. Remember to change the content-type.

[1] https://developers.google.com/drive/api/v3/reference/files

[2] https://docs.python.org/3/library/functions.html#open

[3] https://www.w3schools.com/python/python_file_open.asp

这篇关于如何使用python将断点续传文件上传到Google云端硬盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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