0

I am trying to download a file or folder from my gitlab repository, but they only way I have seen to do it is using CURL and command line. Is there any way to download files from the repository with just the python-gitlab API? I have read through the API and have not found anything, but other posts said it was possible, just gave no solution.

  • `id = [d['id'] for d in p.repository_tree() if d['name'] == 'README.rst'][0]` `file_content = p.repository_raw_blob(id)` [docs](https://python-gitlab.readthedocs.io/en/stable/gl_objects/projects.html) – Lei Yang Jun 29 '21 at 14:26
  • @LeiYang is there a way to do it from a path to the file on gitlab? I have the exact path of the file I want to download. Also, what if I need to download an entire folder of files? – anonymouscat Jun 29 '21 at 14:31
  • i suggest you use other libraries, such as [gitpython](https://gitpython.readthedocs.io/), or `requests` module to simulate curl. the [doc](https://python-gitlab.readthedocs.io/en/stable/faq.html) says gitlab-python even "doesn’t provide an API to clone a project". – Lei Yang Jun 29 '21 at 14:36

1 Answers1

0

You can do like this:

import requests
response = requests.get('https://<your_path>/file.txt')
data = response.text

and then save the contents (data) as file...

Otherwise use the API:

f = project.files.get(path='<folder>/file.txt',ref='<branch or commit>')

and then decode using:

import base64
content = base64.b64decode(f.content)

and then save content as file...