Python 从post表单将文件上载到google drive

Python 从post表单将文件上载到google drive,python,flask,google-api,google-drive-api,google-api-client,Python,Flask,Google Api,Google Drive Api,Google Api Client,我正在尝试使用post方法从用户表单上载文件: @main.route('/gupload/', methods=['POST']) def google_upload(): Drive.Google.put_file(request.files['file'].filename, request.files['file'].read()) return redirect(url_for('main.update_page')) @staticmethod def put_fi

我正在尝试使用post方法从用户表单上载文件:

@main.route('/gupload/', methods=['POST'])
def google_upload():
    Drive.Google.put_file(request.files['file'].filename, request.files['file'].read())
    return redirect(url_for('main.update_page'))

@staticmethod
def put_file(name, file):
    Google.refresh_token()
    drive_service = build('drive', 'v3', credentials=pickle.loads(session['google']['credentials']))
    body = {
        'name': name,
        'parents': [{'id': Google.get_root_dir_id()}]
    }
    file_media = MediaFileUpload(file)
    responce = drive_service.files().create(body=body, media_body=file_media).execute()
    return responce
但我得到了一个错误:

FileNotFoundError: [Errno 2] No such file or directory: b'{"web":{"client_id":"--secret--","project_id":"intouch-265519","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://oauth2.googleapis.com/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"--secret--","redirect_uris":["http://127.0.0.1:5000/vk/google_login_response"]}}'
我通过以下方式获取文件:
request.files['file'].read()
然后调用我自己的方法
put\u file
,下一步是通过
MediaFileUpload
方法制作媒体体,使用示例:

MediaFileUpload('cow.png', mimetype='image/png',chunksize=1024*1024, resumable=True)

我的问题是,我不知道如何将文件从用户表单传输到google drive api,因为
MediaFileUpload
服务器存储中需要文件,但我无法将文件保存在服务器上,因为它太长。

虽然我不确定这是否是您问题的直接解决方案,但在您的脚本中,使用了drive api v3。那么,将驱动器API v2的
'parents':[{'id':Google.get\u root\u dir\u id()}]
修改为v3的
'parents':[Google.get\u root\u dir\u id()]
?但是我不确定Google.get\u root\u dir\u id()。所以,如果这不能解决你的问题,我道歉。哦,是的,谢谢,我没有注意到这一点!这意味着a在我的代码中有两个问题:)谢谢你的回复。我很高兴你的问题解决了。当您的问题得到解决时,您能否将其作为答案发布?这样,它将对其他有相同问题的用户有用。抱歉,但这并没有解决我的问题。谢谢您的回复。我可以问一下您当前的问题吗?虽然我不确定这是否是您问题的直接解决方案,但在您的脚本中,使用了Drive API v3。那么,将驱动器API v2的
'parents':[{'id':Google.get\u root\u dir\u id()}]
修改为v3的
'parents':[Google.get\u root\u dir\u id()]
?但是我不确定Google.get\u root\u dir\u id()。所以,如果这不能解决你的问题,我道歉。哦,是的,谢谢,我没有注意到这一点!这意味着a在我的代码中有两个问题:)谢谢你的回复。我很高兴你的问题解决了。当您的问题得到解决时,您能否将其作为答案发布?这样,它将对其他有相同问题的用户有用。抱歉,但这并没有解决我的问题。谢谢您的回复。我可以问一下你目前的问题吗?