Skip to main content
 首页 » 编程设计

python之使用 Python 将大于 70 MB 的文件上传到谷歌驱动器

2024年11月01日8itcoder

您好,我尝试使用以下代码将文件上传到 Google 云端硬盘

def upload_file(self,file_name,path): 
 
    parents = None 
    if not path == None: 
        parents = self.create_path(path) 
    mime_type = self.get_mime_type_for(file_name) 
    file_id = self.check_file_exist(file_name,parents,mime_type) 
    if file_id == None: 
 
        print "creating file...........",file_name                       
        print "mime_type",mime_type 
        media = MediaFileUpload(file_name, mimetype=mime_type, resumable=True) 
        body = { 
            'title': file_name, 
            'description': 'A test document', 
            'mimeType': mime_type 
        } 
        if not parents == None: 
            body['parents'] = [{'id': parents}] 
        f = self.drive_service.files().insert(body=body, media_body=media).execute() 
    else: 
        print "file exists........... updating" 
        self.update_file(file_id, file_name) 

此代码适用于较小的文件(测试最大 25MB)。但是如果我尝试上传大文件(70MB),系统会给出错误信息

追溯(最近的调用最后):

File "googledrive.py", line 176, in

gd.upload_file("test.mp4","/media/media")

File "googledrive.py", line 122, in upload_file

f = self.drive_service.files().insert(body=body, media_body=media).execute()

File "/usr/local/lib/python2.7/dist-packages/oauth2client/util.py", line 132, in positional_wrapper

return wrapped(*args, **kwargs)

File "/usr/local/lib/python2.7/dist-packages/apiclient/http.py", line 688, in execute

_, body = self.next_chunk(http=http, num_retries=num_retries)

File "/usr/local/lib/python2.7/dist-packages/oauth2client/util.py", line 132, in positional_wrapper

return wrapped(*args, **kwargs)

File "/usr/local/lib/python2.7/dist-packages/apiclient/http.py", line 867, in next_chunk

headers=headers)

File "/usr/local/lib/python2.7/dist-packages/oauth2client/util.py", line 132, in positional_wrapper

return wrapped(*args, **kwargs)

File "/usr/local/lib/python2.7/dist-packages/oauth2client/client.py", line 490, in new_request

redirections, connection_type)

File "/usr/local/lib/python2.7/dist-packages/httplib2/init.py", line 1570, in request

(response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)

File "/usr/local/lib/python2.7/dist-packages/httplib2/init.py", line 1317, in _request

(response, content) = self._conn_request(conn, request_uri, method, body, headers)

File "/usr/local/lib/python2.7/dist-packages/httplib2/init.py", line 1286, in _conn_request

response = conn.getresponse()

File "/usr/lib/python2.7/httplib.py", line 1045, in getresponse

response.begin()

File "/usr/lib/python2.7/httplib.py", line 409, in begin

version, status, reason = self._read_status()

File "/usr/lib/python2.7/httplib.py", line 373, in _read_status

raise BadStatusLine(line)

httplib.BadStatusLine: '' 

请您参考如下方法:

如果您的上传时间超过大约一个小时,您的 token 可能会过期并且您的下载将失败。这是一个已知的 issue .

另请参阅 Google Mirror API throwing BadStatusLine exception (Python)