3

您好我尝试使用以下代码将文件上传到 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),系统会给出错误信息

回溯(最近一次通话最后):

文件“googledrive.py”,第 176 行,在

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

文件“googledrive.py”,第 122 行,在 upload_file

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

文件“/usr/local/lib/python2.7/dist-packages/oauth2client/util.py”,第 132 行,位于 positional_wrapper

返回包装(*args,**kwargs)

文件“/usr/local/lib/python2.7/dist-packages/apiclient/http.py”,第 688 行,在执行中

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

文件“/usr/local/lib/python2.7/dist-packages/oauth2client/util.py”,第 132 行,位于 positional_wrapper

返回包装(*args,**kwargs)

文件“/usr/local/lib/python2.7/dist-packages/apiclient/http.py”,第 867 行,在 next_chunk

标题=标题)

文件“/usr/local/lib/python2.7/dist-packages/oauth2client/util.py”,第 132 行,位于 positional_wrapper

返回包装(*args,**kwargs)

文件“/usr/local/lib/python2.7/dist-packages/oauth2client/client.py”,第 490 行,在 new_request

重定向,连接类型)

文件“/usr/local/lib/python2.7/dist-packages/httplib2/init .py ”,第 1570 行,在请求中

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

_request中的文件“/usr/local/lib/python2.7/dist-packages/httplib2/init .py”,第 1317 行

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

_conn_request中的文件“/usr/local/lib/python2.7/dist-packages/httplib2/init .py”,第 1286 行

响应 = conn.getresponse()

getresponse 中的文件“/usr/lib/python2.7/httplib.py”,第 1045 行

response.begin()

文件“/usr/lib/python2.7/httplib.py”,第 409 行,开始

版本、状态、原因 = self._read_status()

_read_status 中的文件“/usr/lib/python2.7/httplib.py”,第 373 行

提高 BadStatusLine(线)

httplib.BadStatusLine: ''
4

1 回答 1

2

如果您的上传时间超过一个小时,您的令牌可能会过期并且您的下载将失败。这是一个已知问题

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

于 2013-08-15T09:57:56.243 回答