【发布时间】:2018-01-23 10:47:36
【问题描述】:
我正在使用 PyDrive 将文件从一个 GDrive 帐户上传到另一个帐户。我可以很好地下载文件,但是当我上传它时,我得到了 400。
pydrive.files.ApiRequestError: <HttpError 400 when requesting https://www.googleapis.com/upload/drive/v2/files?alt=json&uploadType=resumable returned "Bad Request">
这是 ListFolder 代码:
def ListFolder(parent, drive1, drive2):
filecount = 0
filelist=[]
file_list = drive1.ListFile({'q': "'%s' in parents and trashed=false" % parent}).GetList()
for f in file_list:
filecount +=1
print("Still working %s" % filecount)
if f['mimeType']!='application/vnd.google-apps.folder': # if regular file
print("I've found a file - passing %s to download" % f['title'])
download_file(drive1, f, drive2) # Download the file
这里是下载文件代码:
def download_file(drive1, f, drive2):
"""This function will download a file from the source drive1 to the local hard drive, just before uploading it to the destination drive- i.e. drive2"""
dirpath = tempfile.mkdtemp()
print ("Downloading a file %s" % f['title'])
filesource = drive1.CreateFile({'id': f['id']})
filesource.GetContentFile(f['title'], f['mimeType'])
upload_file(drive2, f)
os.remove(f['title'])
drive2 是一个 GoogleDrive 实例(已通过身份验证)。 f 是 file_list 中不是文件夹的项目。
def upload_file(drive2, f):
#Will copy files over until it detects a directory
""""This function accepts the destination drive, filetitle (which has previously been downloaded from download_file function) and the file mimetype. This function uploads the local file to the destination drive"""
filename = f['title']
print("file title: %s" % f['title']) #filetitle)
file = drive2.CreateFile()
file.SetContentFile(f['title'])
file.Upload()
我尝试使用字符串上传文件,但仍然得到相同的结果。我还应该寻找什么?
【问题讨论】:
-
400 Bad Request 表示您为某些属性提供的参数无效。
-
谢谢 - 我已经尝试在该参数中使用常规文件名(作为字符串)并获得相同的效果。
-
事实证明,常规文件上传正常(例如 goats.txt)。但是,我正在尝试使用的文件是 Gdoc - 这些文件是否具有不同的 mimeType 并因此具有上传功能?
-
是的。就“mimeType”而言,Google 文档与 .txt 文件不同。可以找到 google 文件 mimeType 的列表 here。那是“application/vnd.google-apps.document”。
-
另外,它不喜欢文件为空的事实——这可能是一个错误吗?
标签: python google-drive-api pydrive