Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upload a zip file as content for InputGitTreeElement #863

Closed
tsiq-cejas opened this issue Aug 9, 2018 · 11 comments
Closed

Upload a zip file as content for InputGitTreeElement #863

tsiq-cejas opened this issue Aug 9, 2018 · 11 comments
Labels

Comments

@tsiq-cejas
Copy link

This is a question:

I am trying to upload zip files to my repo by creating a tree with a bunch of InputGitTreeElement elements. Some of these InputGitTreeElement are zip files. InputGitTreeElement only accepts 'str' for content. How can I do this?

thanks!

@sfdye
Copy link
Member

sfdye commented Aug 10, 2018

What's your original use case? Just uploading a file to your repo? If so, there is a upload api you can use. (but limited to upload one file at a time)

@tsiq-cejas
Copy link
Author

First, I forgot to say that I am using Python3.
I am want to upload a bunch of changed json files plus some zip files as one commit:

repo.create_git_commit(commit_msg, end_tree, parents=[commit.commit])

end_tree is a list of InputGitTreeElement. Each one corresponds to a file. My problem becomes that the 'content' of InputGitTreeElement can only be of type 'str' or 'unicode'. When I load a file to memory I have type 'bytes'. What is the right way to encode those bytes to str or unicode so InputGitTreeElement can upload my file?

thanks!

@sfdye
Copy link
Member

sfdye commented Aug 10, 2018

You can use blob.

import base64 

blob = repo.create_git_blob("XXX", encoding='utf-8')
blob = repo.get_git_blob(sha=blob.sha)
tree_element = InputGitTreeElement(path='', mode='100644', type='blob', content=base64.b64decode(blob.content).decode('utf-8'))

@tsiq-cejas
Copy link
Author

It wouldn't be the same problem? create_git_blob expects string as content.
url points to a zip downloadable url.

resp = requests.get(url)
blob = repo.create_git_blob(resp.content, encoding='utf-8')
blob = repo.get_git_blob(sha=blob.sha)
tree_element = InputGitTreeElement(path='', mode='100644', type='blob', content=base64.b64decode(blob.content).decode('utf-8'))  


...
    repo.create_git_blob(resp.content, encoding='utf-8')
  File "/usr/local/lib/python3.6/site-packages/github/Repository.py", line 764, in create_git_blob
    assert isinstance(content, str), content
AssertionError: `b'PK\x03\x04\n\x00\x00...`

@sfdye
Copy link
Member

sfdye commented Aug 10, 2018

replace resp.content with resp.content.decode("utf-8")

@tsiq-cejas
Copy link
Author

tsiq-cejas commented Aug 10, 2018

Is not able to decode it as utf-8:

blob = repo.create_git_blob(resp.content.decode('utf-8'), encoding='utf-8')
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x9c in position 43: invalid start byte

It is a zip file. Should be expected to be encoded in utf-8?

@stale
Copy link

stale bot commented Oct 9, 2018

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Oct 9, 2018
@stale stale bot closed this as completed Oct 16, 2018
@cosme12
Copy link

cosme12 commented Oct 21, 2018

Did you manage to solve this? I'm having the same problem. I can't find a way to upload a png file.

@asfixia
Copy link

asfixia commented Jul 16, 2019

I have the same problem, i cant send a simple image file.
This issue is close to this i guess: #621

@smbanaie
Copy link

smbanaie commented Aug 3, 2019

You Can Try This :

data = base64.b64encode(open(file_name, "rb").read())
path = "{}/{}/{}".format(now.year, now.month, "tweets.zip")
blob = repo.create_git_blob(data.decode("utf-8"), "base64")
element = InputGitTreeElement(path=path, mode='100644', type='blob', sha=blob.sha)
element_list.append(element)
tree = repo.create_git_tree(element_list, base_tree)
parent = repo.get_git_commit(master_sha)
commit = repo.create_git_commit("commit_message", tree, [parent])
master_ref.edit(commit.sha)```

@OpetherMB
Copy link

is there any possible solution to this thread?
when i try to push my zip file i get this error :

Traceback (most recent call last): File "/home/aird/Documents/bdd/1_week_road/push_data.py", line 44, in <module> element = InputGitTreeElement(file_names[i], '100644', 'blob', content=base64.b64decode(data).decode('utf-8')) UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa3 in position 1: invalid start byte

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants