【问题标题】:Uploading file to Google Drive using resumable API使用可恢复 API 将文件上传到 Google Drive
【发布时间】:2020-03-04 14:53:43
【问题描述】:

我有一个非常大的文件,我正在尝试使用 API 上传到谷歌驱动器。我正在尝试使用示例图像来进行学习。将图像上传为 multiplart 上传或单个文件上传可以毫不犹豫地工作,但是当我尝试使用可恢复上传端点进行上传时,代码给了我一个错误:

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "badContent",
    "message": "Unsupported content with type: image/jpeg"
   }
  ],
  "code": 400,
  "message": "Unsupported content with type: image/jpeg"
 }
}

我使用的代码如下:

import requests
import os
filesize = os.path.getsize('./photo.jpeg')
print("File size is: ", filesize)

headers = {"Authorization" : "Bearer "+"<MY API KEY HERE>",
        "Content-Length": str(filesize),
        "Content-Type": "image/jpeg"}
params = {
        "name": "sample.png",
        "parents": ['1CxrbEfy5y3ZyBVF6k2IFIuOk_Z0wjZAo']
        }
files  = {
        'data': ('metadata', json.dumps(params), 'image/jpeg'),
        'file': open('./photo.jpeg', 'rb')
        }

r = requests.post(
        "https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable",
        headers = headers,
        files = files
        )
print(r.text)

请帮忙。

【问题讨论】:

  • 如果我将 POST url 修改为 "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart",,那么它会按预期工作。 ``` { "kind": "drive#file", "id": "1oorSysJXM0y14LrEr-GL2_rCq4pC3kfH", "name": "Untitled", "mimeType": "application/json" } ```

标签: python google-drive-api


【解决方案1】:
  • 您想使用可续传上传photo.jpeg 的图像文件。
  • 您想通过 python 使用requests 来实现此目的。
  • 您的访问令牌可以将文件上传到 Google 云端硬盘。

如果我的理解是正确的,那么这个答案呢?请认为这只是几个可能的答案之一。

修改点:

  • 在您的脚本中,

    • params 未使用。
    • 文件直接发送至https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable
    • "Content-Type": "image/jpeg" 用于请求标头中。这样,错误就发生了。
    • 您尝试上传'./photo.jpeg'。但是您尝试将文件名设置为sample.png
  • 为了使用可续传上传文件,首先需要检索location,这是上传文件的URL。这包含在响应标头中。 Ref

  • 检索到location后,可以将数据上传到location的端点。

示例脚本:

在以下示例脚本中,图像 (image/jpeg) 与可恢复上传一起上传。在这种情况下,作为一个简单的测试,文件是按一个块上传的。使用前请先设置access_tokenfilename的变量

import json
import os
import requests

access_token = '###'  # Please set your access token.
filename = './photo.jpeg'  # Please set the filename with path.

filesize = os.path.getsize(filename)
print("File size is: ", filesize)

# 1. Retrieve session for resumable upload.
headers = {"Authorization": "Bearer "+access_token, "Content-Type": "application/json"}
params = {
    "name": "sample.jpeg",
    "parents": ['1CxrbEfy5y3ZyBVF6k2IFIuOk_Z0wjZAo'],
    "mimeType": "image/jpeg"
}
r = requests.post(
    "https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable",
    headers=headers,
    data=json.dumps(params)
)
location = r.headers['Location']

# 2. Upload the file.
headers = {"Content-Range": "bytes 0-" + str(filesize - 1) + "/" + str(filesize)}
r = requests.put(location, headers=headers, data=open(filename, 'rb'))
print(r.text)
  • 请再次确认1CxrbEfy5y3ZyBVF6k2IFIuOk_Z0wjZAo的父ID是否正确。

参考:

如果我误解了您的问题并且这不是您想要的方向,我深表歉意。

【讨论】:

  • 非常感谢!代码有效!我现在正在浏览每个部分:)
  • @Sandesh Bhusal 感谢您的回复。很高兴您的问题得到解决。
猜你喜欢
  • 2016-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多