使用.get_contents()
方法尝试下载大文件会引发错误:
{'errors': [{'code': 'too_large', 'field': 'data',
'resource': 'Blob'}],
'message': 'This API returns blobs up to 1 MB in size. The requested blob is too large to fetch via the API, but you can use the Git Data API to request blobs up to 100 MB in size.',
'documentation_url': 'https://developer.github.com/v3/repos/contents/#get-contents'}
有没有一种方法可以检测到这种情况并将其传递到可以下载文件的另一个处理程序?
例如,如果类似的操作失败:
contents = repository.get_dir_contents(urllib.parse.quote(server_path), ref=sha)
for content in contents:
if content.type != 'dir':
file_content = repository.get_contents(urllib.parse.quote(content.path), ref=sha)
(可选)还原为:
file_content = repository.get_git_blob(content.sha)
我也曾经遇到过这个问题。 就我而言,由于我始终具有斑点的SHA,因此我只使用git_git_blob
。
但是, get_git_blob
除blob
(因此具有名称)以外,不适用于任何对象类型。 在尝试调用对象之前,您需要知道对象的类型。
要进行后备,您需要了解以下两条信息:
如果get_contents
失败,则不会告诉您这两种情况。 据我所知,实际上并没有任何好的方法来进行后备。
关闭为wontfix
。 如果有人对如何解决这个问题有个好主意,我很高兴重新开放。 据我所知,这看起来不可能以一种干净的方式进行。
我也有同样的问题,最终还是做了一些类似的事情。
file_contents = repo.get_contents(dir_name, ref=branch)
然后每个file_content
sha
存在file_content
,以下内容可用于获取每个文件的blob
for file_content in file_contents:
try:
if file_content.encoding != 'base64':
# some error ...
# ok...
except GithubException:
# if file_content DOES NOT HAVE encoding, it is a large file
blob = repo.get_git_blob(file_content.sha)
# do something with blob
如果path_name
是指大于1M的单个文件,则它必须是一些try / exception块,如下所示:
try:
res = repo.get_contents(path_name, ref=branch)
# ok, we have the content
except GithubException:
return get_blob_content(repo, branch, path_name)
其中get_blob_content
就像
def get_blob_content(repo, branch, path_name):
# first get the branch reference
ref = repo.get_git_ref(f'heads/{branch}')
# then get the tree
tree = repo.get_git_tree(ref.object.sha, recursive='/' in path_name).tree
# look for path in tree
sha = [x.sha for x in tree if x.path == path_name]
if not sha:
# well, not found..
return None
# we have sha
return repo.get_git_blob(sha[0])
带有错误检查的实际代码更长,但是这里有个主意。
当获得Blob时,以下代码将很有用。
blob = repo.get_git_blob(sha[0])
b64 = base64.b64decode(blob.content)
return b64.decode("utf8")
此外,更新文件也将遇到此问题。
raise self.__createException(status, responseHeaders, output)
github.GithubException.UnknownObjectException:404 {“ message”:“未找到”,“ documentation_url”:“ https://docs.github.com/rest/reference/repos#get -repository-content”}尝试时出现此错误为主分支下载已获取的存储库文件
最有用的评论
我也有同样的问题,最终还是做了一些类似的事情。
然后每个
file_content
sha
存在file_content
,以下内容可用于获取每个文件的blob如果
path_name
是指大于1M的单个文件,则它必须是一些try / exception块,如下所示:其中
get_blob_content
就像带有错误检查的实际代码更长,但是这里有个主意。