2

I'd like to find an easier way to grab a file from a private GitHub repository.

I managed to download a file via curl by creating a personal access token on GitHub, and then using it like this:

curl https://[email protected]/accountName/repoName/branchName/fileName.txt

This works for me, however I was wondering if there's an easier way, perhaps using SSH? I wouldn't mind having to type my SSH password as it seems more convenient than creating an access token. I can already authenticate successfully on GitHub:

$ ssh -T [email protected]
# Attempts to ssh to GitHub

However, when I try using curl with an SSH URL, I get this error:

curl ssh://[email protected]:accountName/repoName/branchName/fileName.txt
curl: (3) URL using bad/illegal format or missing URL

I tried some other methods described in this article, to no avail.

Any ideas?

2 Answers 2

6

First of all, curl says "bad/illegal format" because you're mixing the URL-style and rsh-style address formats. Traditionally, Git accepts both for git clone, but only the latter puts a : between host and path – whereas in URLs, all paths start with a /. For example, the rsh/scp-style address [email protected]:foo/bar would be written as the URL ssh://[email protected]/foo/bar, just like HTTP URLs.

SSH is not a file transfer protocol on its own – it's more like TLS, something that can carry various file transfer protocols such as SFTP or scp or rsync (much like TLS can carry HTTP). Giving curl an ssh:// URL is meaningless1, but you could give it an sftp:// one to retrieve a file over SFTP. (Note how the article that you linked also specifically uses SFTP.)

However, GitHub does not provide SFTP access; the only thing allowed over SSH connections to GitHub is the Git protocol. That's not something you can access with curl, only with git clone.

So if you must use SSH, then your only option with GitHub is to actually clone the repository via Git. (It is possible to reduce the download size using --depth= or --filter= options, but it still ends up being a whole repository and not just the individual file.)

1 (Git uses ssh:// URLs but the meaning is clear from context – it's the Git protocol, but tunnelled over SSH. Git doesn't use SFTP.)

1
  • Very informative, thank you!
    – Simone
    Commented Nov 14, 2021 at 14:13
0

first of all you need a API access token from github how to create it:

  1. go to github.com and click on your profile picture at top right
  2. scroll down and click settings on the side bar which is shown
  3. scroll down and click on developer settings on left sidebar
  4. click on Personal Access Token(menu bar)
  5. click on tokens(classic)
  6. then click on Generate New Token(menu bar)
  7. then click on generate new token(classic)
  8. write the information of the token(name, expires date) and be careful! allow all of the access which is shown on the page! if you don't the API shouldn't work
  9. copy your API key
  10. write this code with the token you got copy
import requests
from requests.structures import CaseInsensitiveDict
GH_PREFIX = "https://raw.githubusercontent.com"
ORG = "GITHUB_USERNAME"
REPO = "YOUR_REPOSITORY_NAME"
BRANCH = "YOUR_REPOSITORY_BRANCH"
FOLDER = "THE_FOLDER_WHICH_INCLUDE_TEST.txt"
FILE = "THE_FILE_YOU_WANT_TO_ACCESS(TEST.txt)"
url = GH_PREFIX + "/" + ORG + "/" + REPO + "/" + BRANCH + "/" + FOLDER + "/" + FILE
headers = CaseInsensitiveDict()
headers["Authorization"] = "token " + "YOUR_API_ACCESS_TOKEN_HERE"
r = requests.get(url, headers=headers, stream=True) 
first = str(r.content).replace("b'", "")
second = first.replace("\\r", "")
third = second.replace("\\n'", "")
result = third.replace("'", "")
print(result)

i hope this helps you

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .