github - 如何从 GitHub 草稿下载文件

标签 github github-api appveyor

我正在使用 AppVeyor 为 GitHub 存储库设置 CI,并将构建工件上传到名为 CI builds 的草稿。该文件是例如位于

https://github.com/an_organisation/a_project/releases/tag/untagged-1111aaaacccc0000dddd/filename.tar.gz

并且可以通过浏览器访问和下载。

现在我想访问从另一个 AppVeyor 项目(即 appveyor.yml 脚本)上传的工件。我尝试使用以下命令使用 AppVeyor DownloadFile 命令、curlwget 下载,但没有成功

  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/

相关文章:

visual-studio - 如何在一个解决方案中实现多个项目的持续集成和部署?

python - “Choco install python”在 AppVeyor 上失败,出现 1603

git - Google App Engine 推送从 GitHub for Windows(和 git bash)部署

github - 通过 API 在 GitHub 存储库中查找最旧的提交

graphql - Github v4 GraphQL API。按日期获取提交

php - 手动安装Elasticsearch的PHP API库

linux - AppVeyor - Ubuntu 未被识别为图像

java - 从 Github 下载 JAVA 代码后,类文件始终被锁定,我永远无法编译或运行我的代码

python - 如何删除github上的文件或文件夹?

github - Jenkins 没有在第一次推送时建立新的分支