【问题标题】:How can I download a single raw file from a private github repo using the command line?如何使用命令行从私有 github 存储库下载单个原始文件?
【发布时间】:2013-08-10 04:44:19
【问题描述】:

在 CI 服务器上,我想获取我们在 Github 上维护的配置文件,以便可以在许多作业之间共享。我正在尝试通过 curl 获取此文件,但这些方法都失败了(我得到 404):

# As advised by the oAuth docs
curl -H 'Authorization: token the_token' -L -o setup.sh https://raw.github.com/org/repo/file

# The url of the raw file after clicking to view it
curl -L https://raw.github.com/org/repo/file?login=username&token=the_token 

【问题讨论】:

    标签: curl github oauth


    【解决方案1】:

    以前的答案不起作用(或不再起作用)。

    您可以使用 V3 API 来获取这样的原始文件(您需要一个 OAuth 令牌):

    curl -H 'Authorization: token INSERTACCESSTOKENHERE' \
      -H 'Accept: application/vnd.github.v3.raw' \
      -O \
      -L https://api.github.com/repos/owner/repo/contents/path
    

    所有这些都必须放在一条线上。 -O 选项将文件保存在当前目录中。您可以使用-o filename 指定不同的文件名。

    要获取 OAuth 令牌,请按照此处的说明进行操作:

    我也把它写成了一个要点:

    编辑:解决方案的 API 参考如下:

    【讨论】:

    • 请注意,如果文件是公开的,则不需要授权令牌:curl -H 'Accept: application/vnd.github.v3.raw' https://api.github.com/repos/owner/repo/contents/path 将返回原始文件。
    • -H 'Accept: application/vnd.github.v3.raw' 有必要吗?没有那个部分我可以访问一个私人文件。
    • 请注意,该 URL 与您在浏览器中使用的 URL 不同。我在这里强调了不同之处:https:// api. github.com/ repos/ <owner>/<repo>/ contents/ <path/to/file> (抱歉有点乱)
    • 这也适用于personal access token。所需的最小权限集是 repoadmin:org/read:org(在私有存储库上)。
    【解决方案2】:

    或者,您可以使用 github“个人访问令牌”(https://github.com/settings/tokens):

    TOKEN=...
    curl -s https://$TOKEN@raw.githubusercontent.com/<user or organization>/<repo name>/<branch>/<path to file>/<file_name>
    

    例子:

    $ curl -s https://1bacnotmyrealtoken123beefbea@raw.githubusercontent.com/concourse/concourse/master/README.md
    ....
    

    【讨论】:

    • @EM0 -- 我刚试过,它奏效了。有几点值得仔细检查:1. 主机部分是raw.githubusercontent.com,2. 路径是&lt;username&gt;/&lt;repo name&gt;/&lt;branch&gt;/&lt;file name&gt; 3. 令牌需要有repo 访问范围。
    • 是的,这就是路径。我从文件的“下载”链接中获取了路径,但从末尾删除了“?token = ...”并添加了令牌。它确实具有 repo 访问范围,但这仅涉及公共存储库。这是一个组织私有存储库。此外,我们启用了 2 因素身份验证,但我认为如果这是它应该给出错误 401,而不是 404 的问题。
    • 是的,这听起来不错。路径听起来不错(这是我单击“原始”时得到的路径,去掉了?token=... 参数,就像你说的那样)。我的用户也有 2 因素身份验证,我假设我们谈论的是相同的令牌范围(github.com/settings/tokens/new 上的 repo 复选框)。对于它的价值,如果令牌无效,或者没有repo 范围,您将得到 404(而不是 401)。不知道为什么这在您的设置中不起作用...
    • 奇怪的事情:对我来说使用上面的curl 命令有效,但是如果我在浏览器中打开相同的链接或尝试通过java.net.URL.openStream 请求它,我得到一个 404...
    • 这是我可以让它在 CMD 中为内部 GitHub 实例工作的唯一方法。在我身上使用curl -H 'Authorization: token $TOKEN' $file_url 总是 404'd。我不确定为什么一个有效而另一个无效,但我从未深入研究 CURL 的文档。
    【解决方案3】:

    我知道这是一个老问题,但上面提出的解决方案都不适合我。也许从那时起 API 发生了变化。

    这行得通:

    curl -H 'Authorization: token [insert your token here]' -o output.txt https://raw.githubusercontent.com/[organization]/[repo]/[branch]/[path to file]

    【讨论】:

    • 这也是唯一对我有用的,但你的答案标记中有一个小错字。应该是[organization]/[repo]/[branch]...
    • 谢谢,Github Enterprise 唯一对我有用的东西。请注意,所需的令牌是个人访问令牌。
    • @OliverPearmain 你试过curl -s https://PAT_VALUE@raw.github.company.com/OrgOrUser/RepoName/BranchOrCommitID/file_name.file_extension 吗?这是我唯一可以开始工作的事情,但是公司中的其他人使用了这个解决方案,所以我认为 CURL 可能有某种配置可以减少使用 curl -s... 而不是这个 CLI 集的限制参数。
    【解决方案4】:

    我为此苦苦挣扎了几分钟,直到我意识到所需要的只是将 url 用引号括起来以逃避 & 符号。

    curl "https://raw.github.com/org/repo/file?login=username&token=the_token"
    

    这在我的私人仓库中对我有用。

    【讨论】:

      【解决方案5】:

      或者,如果您没有令牌:

      curl --user [your_user] 'https://raw.github.com/path/to/file.config' > file.config
      

      【讨论】:

      • 我被要求输入密码,但响应始终是 404。
      • 此处相同:始终为 404
      【解决方案6】:

      当 url 被重定向到 Amazon S3 时,我遇到了身份验证错误:

      只允许一种身份验证机制;只有X-Amz-Algorithm 查询参数...

      Authorization: token X 标头更改为 ?access_token=&lt;token&gt; 查询参数对我有用。

      【讨论】:

        【解决方案7】:
        1. 在浏览器中打开你的 github 仓库:点击文件
        2. 在浏览器中打开开发者工具:选择网络标签
        3. 在浏览器 github 中:点击下载按钮
        4. 关闭弹出窗口
        5. 在浏览器开发工具中:右键单击具有 file_name?token=ABAHQCAT6KG...
        6. 选择复制->复制链接地址

          网址格式:

          https://raw.githubusercontent.com/&lt;USERNAME&gt;/&lt;PATH&gt;/&lt;FILENAME&gt;?token=ABAHQCAT6KGHYHMG2SLCDT243PH4I

        7. 在终端:

          wget -O myFilename https://raw.githubusercontent.com/&lt;USERNAME&gt;/&lt;PATH&gt;/&lt;FILENAME&gt;?token=ABAHQCAT6KGHYHMG2SLCDT243PH4I

        链接仅在有限的时间内有效,或者您可以创建您的令牌:GitHub article

        【讨论】:

          【解决方案8】:

          恕我直言,一个更简单的解决方案是使用Official GitHub CLIgh

          1. 首先您必须登录:
          gh auth login
          

          对我来说,这个命令不是必需的,因为我已经登录了。

          1. 然后我们需要针对要下载的文件的 API URL。并调用gh将其转换为认证下载地址:
          API_URL=https://api.github.com/repos/owner/repo/contents/path/file.ext
          curl $(gh api $API_URL --jq .download_url) -o file.ext
          

          一个真实的例子可能更好。这里是从ghcli下载install_linux.md

          API_URL=https://api.github.com/repos/cli/cli/contents/docs/install_linux.md
          curl $(gh api $API_URL --jq .download_url) -o install_linux.md
          

          API_URL:

          • 用户ownercli
          • 存储库名称repo 也是cli
          • 文件路径 (path/file.ext) 是 docs/install_linux.md

          【讨论】:

          • 为避免安装jq,您可以将其作为选项传递:curl $(gh api $API_URL --jq .download_url) -o file.ext
          • @BertrandPestre 感谢您的技巧。这是一个非常新的选项:8 天前!
          【解决方案9】:

          我们不得不经常从私有 GitHub 存储库下载文件,而 hacky 的 shell 脚本并没有完全解决它,所以我们创建了 fetch,这是一个开源的跨平台工具,可以轻松下载源文件并从公共和私有 GitHub 存储库的 git 标签、提交或分支发布资产。

          例如,要将文件 baz 从私有 GitHub 存储库的版本 0.1.3 下载到 /tmp,您需要执行以下操作:

          GITHUB_OAUTH_TOKEN="your token"
          fetch --repo="https://github.com/foo/bar" --tag="0.1.3" --source-path="/baz" /tmp
          

          【讨论】:

            【解决方案10】:

            只是对已接受答案的补充,如果您使用的是 Github Enterprise url,则略有不同:

            curl -H 'Authorization: token [your token]' \
            -H 'Accept: application/vnd.github.v3.raw' \
            -L https://[your domain]/api/v3/repos/[owner]/[repo-name]/contents/[path of file]
            

            【讨论】:

              【解决方案11】:

              令人惊讶的是,在我找到解决方法之前,没有一个答案对我有用。

              您可以使用@thomasfuchs 回答的个人访问令牌https://github.com/settings/tokens

              注意:创建令牌时,您必须检查管理员权限。查看相关问题

              https://github.com/octokit/octokit.net/issues/1812

              【讨论】:

              • 管理员提示让它为我工作。否则我刚刚收到 404。
              • 使其在私人仓库中为我工作的最小权限集是 repoadmin:org/read:org
              【解决方案12】:

              我能够让它为 github 企业工作,感谢上面的建议。不得不接受你所有的建议并尝试,最后我能够让它发挥作用。这些是我为其工作所遵循的步骤。

              1. 创建个人令牌,按照以下步骤操作:

              https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token

              1. 确保您对令牌具有最低以下权限:

                • repo(在 repo 下全选)
                • admin:org -> read:org(在“admin:org”下选择“read:org”)
              2. 使用以下 curl 命令获取内容:

              curl -H "Authorization: token [yourPersonalToken]" -H "Accept: application/vnd.github.v3.raw" -o [filePath]-content.json -L https://github.[company].com/api/v3/repos/[ORG]/[REPO_NAME]/contents/[PATH_TO_FILE]/content.json?ref=[BRANCH_NAME]
              

              在哪里->

               [yourPersonalToken] is the token you created.
               [filePath] is a path where you want to save the downloaded copy.
               [company] is the name of company which hosted the github enterprise.
               [ORG] is the github organization is which repo is created.
               [REPO_NAME] is the name of the repository.
               [PATH_TO_FILE] is the path where file is located.
               [BRANCH_NAME] is the name of the branch you want to use, e.g. master, develop etc.
              

              例子:

              curl -H "Authorization: token 5a86ecda9ff927baaa66fad2af5bee8" -H "Accept: application/vnd.github.v3.raw" -o C:\Downloads\manifest.json -L https://github.example.com/api/v3/repos/cms/cms_one/contents/app/data/manifest.json?ref=master
              

              【讨论】:

              • 该 API 可以下载小于 1 MB 的文件,截至今天。如果我们需要下载一个大文件,使用这个方法:caludio.medium.com/…
              【解决方案13】:
              curl -H 'Authorization: token YOUR_TOKEN' \
                -H 'Accept: application/vnd.github.v4.raw' \
                -O \
                -L https://api.github.com/repos/INSERT_OWNER_HERE/INSERT_REPO_HERE/contents/PATH/TO/FILE
              

              所以如果原始文件的 url(登录时)是

              https://raw.githubusercontent.com/mr_coder/my_repo_name/master/my_script
              
              
              Then 
                -L https://api.github.com/repos/INSERT_OWNER_HERE/INSERT_REPO_HERE/contents/PATH/TO/FILE
              becomes
                -L https://api.github.com/repos/mr_coder/my_repo_name/contents/my_script
              

              注意:我们有 API v4

              【讨论】:

                【解决方案14】:

                对于 GitHub Enterprise 和 API v3,我的 bash 解决方案如下所示(包括 TOKEN 清理/隐私):

                TOKEN=yourTokenHere; history -d $((HISTCMD-1)) > /dev/null
                
                curl -H "Authorization: token $TOKEN" \
                  -H 'Accept: application/vnd.github.v3.raw' \
                  -o file.ext \
                  -L http://github.company.com/api/v3/repos/[org]/[repo]/contents/path/file.ext?ref=[branch]
                
                unset TOKEN
                

                【讨论】:

                  【解决方案15】:

                  我认为发行一个可以访问所有存储库的个人访问令牌(即使只是从我的私人存储库下载单个文件)有点危险且不是好方法。

                  如何 -

                  我很乐意推荐使用 url 和单个文件的令牌。别担心。令牌字符串将由 github 自动生成。您可以在您的源代码页面上获取此网址。

                  1. 通过 curl 或 wget 等方式转到要下载的源代码页面
                  2. 找到“原始”按钮并单击它。
                  3. 新页面打开,只需复制 url。该网址如下所示:
                    (https://raw.githubusercontent.com/USERNAME/REPONAME/BRANCHNAME/FILENAME?token=TOKENSTRING)。
                  4. 您可以使用此网址下载文件

                  【讨论】:

                  【解决方案16】:

                  我尝试了一个简单的技巧,在 Pycharm 和 Colab 中打开 GitHub 私有 .iypnb 文件,它对我来说效果很好。

                  1. 按原始按钮获取 .ipynb 文件的原始文本,这将打开 像这样的一些文字。
                  {
                   "cells": [
                    {
                     "cell_type": "code",
                     "execution_count": 2,
                     "metadata": {},
                     "outputs": [],
                     "source": []
                  }]
                  }
                  
                  1. 在操作系统(例如windows)上打开记事本/文本编辑器,将所有文本复制到一个新的记事本文件中。

                  2. 将记事本保存为 name.ipynb 而不是 name.txt 并将保存为文件类型 All Files(.) 而不是 Text Documents (*.txt)

                  3. 最终在您的 IDE 或 colab 中打开文件。

                  【讨论】:

                    【解决方案17】:

                    下面应该可以正常工作。分支名称前的“原始”(在本例中为 master)。

                    curl -L -O https://github.com/your/repo/raw/master/fetch_file.sh

                    【讨论】:

                    • 这个问题是关于私人回购的
                    【解决方案18】:

                    您可以使用原始链接来做到这一点。

                    curl -O https://raw.githubusercontent.com/owner/repo/branchname/path/to/file
                    

                    【讨论】:

                    • 问题是关于私人回购
                    猜你喜欢
                    • 2022-11-11
                    • 2013-12-22
                    • 2020-12-09
                    • 2016-05-30
                    • 1970-01-01
                    • 1970-01-01
                    • 2021-11-02
                    • 1970-01-01
                    • 2017-02-03
                    相关资源
                    最近更新 更多