gpt4 book ai didi

python - pydrive上传文件400 "Bad Request"

转载 作者:太空宇宙 更新时间:2023-11-03 14:12:32 25 4
gpt4 key购买 nike

我正在使用 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()

我尝试使用字符串上传文件,但仍然得到相同的结果。我还应该寻找什么?

最佳答案

这是来自 export files guide 的下载片段:

file_id = '1ZdR3L3qP4Bkq8noWLJHSr_iBau0DNT4Kli4SxNc2YEo'
request = drive_service.files().export_media(fileId=file_id,
mimeType='application/vnd.google-apps.document')
fh = io.BytesIO()
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
status, done = downloader.next_chunk()
print "Download %d%%." % int(status.progress() * 100)

关于python - pydrive上传文件400 "Bad Request",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48399789/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com