【发布时间】: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" } ```