1 回答
TA贡献1851条经验 获得超5个赞
问题和解决方法:
使用的时候resumable=True
,好像0
不能使用byte的数据。所以在这种情况下,需要上传空数据而不使用resumable=True
. 但是当我看到PyDrive的脚本时,似乎它被resumable=True
用作默认值。参考所以在这种情况下,作为一种解决方法,我想建议使用该requests
模块。访问令牌是从gauth
PyDrive 检索的。
当你的脚本被修改后,它会变成如下所示。
修改后的脚本:
import io
import requests
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
gauth = GoogleAuth()
gauth.LocalWebserverAuth()
drive = GoogleDrive(gauth)
file = drive.CreateFile({'title': 'test.txt'})
file.Upload()
file.SetContentString('hello')
file.Upload()
# file.SetContentString()
# file.Upload() # This throws an exception.
# I added below script.
res = requests.patch(
"https://www.googleapis.com/upload/drive/v3/files/" + file['id'] + "?uploadType=multipart",
headers={"Authorization": "Bearer " + gauth.credentials.token_response['access_token']},
files={
'data': ('metadata', '{}', 'application/json'),
'file': io.BytesIO()
}
)
print(res.text)
添加回答
举报