我正在使用 AppVeyor 为 GitHub 存储库设置 CI,并将构建工件上传到名为 CI builds 的草稿。该文件是例如位于
https://github.com/an_organisation/a_project/releases/tag/untagged-1111aaaacccc0000dddd/filename.tar.gz
并且可以通过浏览器访问和下载。
现在我想访问从另一个 AppVeyor 项目(即 appveyor.yml 脚本)上传的工件。我尝试使用以下命令使用 AppVeyor DownloadFile
命令、curl
和 wget
下载,但没有成功
set DOWNLOAD_FILENAME=filename.tar.gz
set DOWNLOAD_ADDRESS=https://github.com/an_organisation/a_project/releases/download/untagged-1111aaaacccc0000dddd/$DOWNLOAD_FILENAME
wget --header "Authorization: token $GH_AUTH_TOKEN" --output-document=$DOWNLOAD_FILENAME $DOWNLOAD_ADDRESS
wget --auth-no-challenge --header "Accept:application/octet-stream" --output-document=$DOWNLOAD_FILENAME "$DOWNLOAD_ADDRESS?access_token:$GH_AUTH_TOKEN"
curl -fsSL -G --user "$APPVEYOR_ACCOUNT_NAME:$GH_AUTH_TOKEN" -o $DOWNLOAD_FILENAME $DOWNLOAD_ADDRESS
curl -fsSL -G -H "Authorization: token $GH_AUTH_TOKEN" -H "Accept: application/octet-stream" -o $DOWNLOAD_FILENAME $DOWNLOAD_ADDRESS
curl -fsSL -G -H "Authorization: token $GH_AUTH_TOKEN" -H "Accept: application/octet-stream" -o $DOWNLOAD_FILENAME https://api.github.com/repos/an_organisation/a_project/releases/download/untagged-1111aaaacccc0000dddd/
慢慢地,我感觉到无法通过 GitHub API 或下载链接从草稿下载文件。
下载此类文件的正确命令是什么?
最佳答案
TLDR 使用Get Release asset API带有标题 Accept: application/octet-stream
:
curl -OJ -L -H "Accept: application/octet-stream" \
-H "Authorization: Token $YOUR_TOKEN" \
"https://api.github.com/repos/$REPO/releases/assets/$ASSET_ID"
您需要拥有 assetID。为了获得它,您需要releaseID(如果您还没有此信息),请使用 GET /repos/:user/:repo/releases
:
curl -s -H "Authorization: Token $YOUR_TOKEN" \
"https://api.github.com/repos/$REPO/releases" | jq '.[] | {(.name): .id}'
然后使用GET /repos/:user/:repo/releases/:release_id
获取 Assets ID :
curl -s -H "Authorization: Token $YOUR_TOKEN" \
"https://api.github.com/repos/$REPO/releases/$RELEASE_ID" | \
jq -r '.assets[] | {(.id |tostring): .name}'
一旦你有了 assetID(顺便说一句,也许你已经有了它),你终于可以使用 GET /repos/:user/:repo/releases/assets/:asset_id
带有标题接受:application/octet-stream
。来自 the documentation :
To download the asset's binary content, set the Accept header of the request to application/octet-stream. The API will either redirect the client to the location, or stream it directly if possible. API clients should handle both a 200 or 302 response.
以下将文件下载到本地:
curl -OJ -L -H "Accept: application/octet-stream" \
-H "Authorization: Token $YOUR_TOKEN" \
"https://api.github.com/repos/$REPO/releases/assets/$ASSET_ID"
关于github - 如何从 GitHub 草稿下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60792587/