.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)
λλ μ μλμ΄ λ¬Έμ μ λΆλͺμ³€λ€. μ κ²½μ°μλ νμ blobμ SHAκ° μμκΈ° λλ¬Έμ λμ git_git_blob
μ¬μ©νμ΅λλ€.
κ·Έλ¬λ get_git_blob
λ blob
(λ°λΌμ μ΄λ¦) μ΄μΈμ κ°μ²΄ μ νμ λν΄ μλνμ§ μμ΅λλ€. νΈμΆμ μλνκΈ° μ μ κ°μ²΄μ μ νμ μμμΌν©λλ€.
ν΄λ°±μ μννλ €λ©΄ λ κ°μ§ μ 보λ₯Ό μμμΌν©λλ€.
get_contents
κ° μ€ν¨νλ©΄μ΄ μ€ μ΄λ κ²λ μλ €μ£Όμ§ μμ΅λλ€. λ΄κ° λ§ν μμλ ν ν΄λ°±μ μννλ μ’μ λ°©λ²μ μμ΅λλ€.
wontfix
λ‘ λ§κ°λμμ΅λλ€. μ΄ λ¬Έμ λ₯Ό ν΄κ²°νλ λ°©λ²μ λν μ’μ μμ΄λμ΄κ°μλ μ¬λμ΄ μμΌλ©΄ λ€μ μ΄μ΄μ κΈ°μ©λλ€. λ΄κ° λ§ν μμλ ν, κ·Έκ²μ κΉ¨λν λ°©μμΌλ‘ ν μμλ κ²μ²λΌ 보μ΄μ§ μμ΅λλ€.
λλ λκ°μ λ¬Έμ κ° μκ³ κ²°κ΅ λκ°λ₯Όνκ²λ©λλ€.
file_contents = repo.get_contents(dir_name, ref=branch)
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": "Not Found", "documentation_url": " https://docs.github.com/rest/reference/repos#get -repository-content"} μλμμ΄ μ€λ₯κ° λ°μν©λλ€. λ§μ€ν° λΈλμΉμ λν μ μ₯μ νμΌμ λ€μ΄λ‘λνλ €λ©΄
κ°μ₯ μ μ©ν λκΈ
λλ λκ°μ λ¬Έμ κ° μκ³ κ²°κ΅ λκ°λ₯Όνκ²λ©λλ€.
sha
κ°file_content
μ λν΄ μ‘΄μ¬νλ©° λ€μμ μ¬μ©νμ¬ κ° νμΌμ blobμ κ°μ Έμ¬ μ μμ΅λλ€.path_name
κ° 1Mλ³΄λ€ ν° λ¨μΌ νμΌμ μ°Έμ‘°νλ κ²½μ° λ€μκ³Ό κ°μ μΌλΆ try / exception λΈλ‘μ΄μ΄μΌν©λλ€.get_blob_content
λ λ€μκ³Ό κ°μ΅λλ€.μ€λ₯ κ²μ¬ κΈ°λ₯μ΄μλ μ€μ μ½λλ λ κΈΈμ§λ§ μμ΄λμ΄λ μ¬κΈ°μ μμ΅λλ€.