要从私有仓库下载发布文件,您可以使用个人访问令牌,该令牌可以在settings/tokens 生成,完全控制私有仓库范围。
然后使用curl 命令下载资产(更改为适当的值):
curl -vLJO -H 'Authorization: token my_access_token' 'https://api.github.com/repos/:owner/:repo/releases/assets/:id'
或者,如果您使用的是 OAuth 应用,请使用:
curl -u my_client_id:my_client_secret https://api.github.com/repos/:owner/:repo/releases/assets/:id
地点:
-
:owner 是您的用户或组织用户名;
-
:repo 是您的存储库名称;
-
:id是你的资产ID,可以在标签发布URL中找到,比如:
https://api.github.com/repos/:owner/:repo/releases/tags/:tag
:token 是您的个人访问令牌(可以在/settings/tokens 创建;
注意:使用access_token 作为查询参数是deprecated。
见:Repositories API v3 at GitHub
这里是 Bash 脚本,它可以根据文件的特定名称下载资产文件:
#!/usr/bin/env bash
# Script to download asset file from tag release using GitHub API v3.
# See: http://stackoverflow.com/a/35688093/55075
CWD="$(cd -P -- "$(dirname -- "$0")" && pwd -P)"
# Check dependencies.
set -e
type curl grep sed tr >&2
xargs=$(which gxargs || which xargs)
# Validate settings.
[ -f ~/.secrets ] && source ~/.secrets
[ "$GITHUB_API_TOKEN" ] || { echo "Error: Please define GITHUB_API_TOKEN variable." >&2; exit 1; }
[ $# -ne 4 ] && { echo "Usage: $0 [owner] [repo] [tag] [name]"; exit 1; }
[ "$TRACE" ] && set -x
read owner repo tag name <<<$@
# Define variables.
GH_API="https://api.github.com"
GH_REPO="$GH_API/repos/$owner/$repo"
GH_TAGS="$GH_REPO/releases/tags/$tag"
AUTH="Authorization: token $GITHUB_API_TOKEN"
WGET_ARGS="--content-disposition --auth-no-challenge --no-cookie"
CURL_ARGS="-LJO#"
# Validate token.
curl -o /dev/null -sH "$AUTH" $GH_REPO || { echo "Error: Invalid repo, token or network issue!"; exit 1; }
# Read asset tags.
response=$(curl -sH "$AUTH" $GH_TAGS)
# Get ID of the asset based on given name.
eval $(echo "$response" | grep -C3 "name.:.\+$name" | grep -w id | tr : = | tr -cd '[[:alnum:]]=')
#id=$(echo "$response" | jq --arg name "$name" '.assets[] | select(.name == $name).id') # If jq is installed, this can be used instead.
[ "$id" ] || { echo "Error: Failed to get asset id, response: $response" | awk 'length($0)<100' >&2; exit 1; }
GH_ASSET="$GH_REPO/releases/assets/$id"
# Download asset file.
echo "Downloading asset..." >&2
curl $CURL_ARGS -H "Authorization: token $GITHUB_API_TOKEN" -H 'Accept: application/octet-stream' "$GH_ASSET"
echo "$0 done." >&2
在运行之前,您需要使用您的 GitHub 令牌设置您的 GITHUB_API_TOKEN(请参阅:/settings/tokens at GH)。这可以放在您的~/.secrets 文件中,例如:
GITHUB_API_TOKEN=XXX
示例脚本用法:
./get_gh_asset.sh :owner :repo :tag :name
其中 name 是您的文件名(或部分文件名)。使用 TRACE=1 前缀的脚本来调试它。
如果您想知道为什么 curl 有时会失败(如其他答案中所述):
只允许一种身份验证机制;只应指定X-Amz-Algorithm 查询参数、签名查询字符串参数或Authorization 标头。
当运行时:
curl -vLJ -H 'Authorization: token <token>' -H 'Accept: application/octet-stream' https://api.github.com/repos/:owner/:repo/releases/assets/<id>
这是因为你同时指定了多种机制,所以S3服务器不知道使用哪一种,所以只能选择一种,比如:
-
X-Amz-Algorithm查询参数
- 签名查询字符串参数(
X-Amz-Signature)
- 授权标头 (
Authorization: token <token>)
并且由于 GitHub 将您从资产页面重定向(在请求 application/octet-stream 时),它会在查询字符串中自动填充凭据,并且由于 curl 在请求标头(您已指定)中传递相同的凭据,因此他们是矛盾的。因此,对于解决方法,您可以改用access_token。