-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Download release assets #2918
Comments
I don't think this library provides a direct method to download assets, but you could use the asset's URL with a HTTP library like PyGithub/github/GitReleaseAsset.py Line 126 in 0b8435f
This is an idea/pseudocode that might help you get started (haven't tested this) from github import Github
import requests
g = Github("your token")
repo = g.get_repo("your repo")
# Get the specific release or terate over releases
release = repo.get_release("tag_name")
# Loop through assets in the release
for asset in release.get_assets():
response = requests.get(asset.browser_download_url, allow_redirects=True)
# whatever you need to do with the binary |
yes it's also missing a way to download workflow artifacts
…On Tue, 12 Mar 2024, 19:27 Xavi Vega, ***@***.***> wrote:
The documentation says that I can download a release assets with:
-
https://docs.github.com/en/rest/releases/assets?apiVersion=2022-11-28#get-a-release-asset
To download the asset's binary content, set the Accept header of the request to [application/octet-stream](https://docs.github.com/rest/overview/media-types). 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.
Can I do it with this library?
I don't think this library provides a direct method to download assets,
but you could use the asset's URL with a HTTP library like requests to
download the asset.
https://github.com/PyGithub/PyGithub/blob/0b8435fccbcc98404f08e146b6e259bd20065c98/github/GitReleaseAsset.py#L126
This is an idea/pseudocode that might help you get started (haven't tested
this)
from github import Githubimport requests
g = Github("your token")repo = g.get_repo("your repo")
# Get the specific release or terate over releasesrelease = repo.get_release("tag_name")
# Loop through assets in the releasefor asset in release.get_assets():
response = requests.get(asset.browser_download_url, allow_redirects=True)
# whatever you need to do with the binary
—
Reply to this email directly, view it on GitHub
<#2918 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AGB6YTALMGEPNFFP4DS7MW3YX5JJHAVCNFSM6AAAAABESEDECOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSOJSGM4DGOJSG4>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
i solved with something similar but was wondering if could be added
…On Tue, 12 Mar 2024, 19:28 nick83ola, ***@***.***> wrote:
yes it's also missing a way to download workflow artifacts
On Tue, 12 Mar 2024, 19:27 Xavi Vega, ***@***.***> wrote:
> The documentation says that I can download a release assets with:
>
> -
> https://docs.github.com/en/rest/releases/assets?apiVersion=2022-11-28#get-a-release-asset
>
> To download the asset's binary content, set the Accept header of the request to [application/octet-stream](https://docs.github.com/rest/overview/media-types). 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.
>
> Can I do it with this library?
>
> I don't think this library provides a direct method to download assets,
> but you could use the asset's URL with a HTTP library like requests to
> download the asset.
>
>
> https://github.com/PyGithub/PyGithub/blob/0b8435fccbcc98404f08e146b6e259bd20065c98/github/GitReleaseAsset.py#L126
>
> This is an idea/pseudocode that might help you get started (haven't
> tested this)
>
> from github import Githubimport requests
> g = Github("your token")repo = g.get_repo("your repo")
> # Get the specific release or terate over releasesrelease = repo.get_release("tag_name")
> # Loop through assets in the releasefor asset in release.get_assets():
> response = requests.get(asset.browser_download_url, allow_redirects=True)
> # whatever you need to do with the binary
>
> —
> Reply to this email directly, view it on GitHub
> <#2918 (comment)>,
> or unsubscribe
> <https://github.com/notifications/unsubscribe-auth/AGB6YTALMGEPNFFP4DS7MW3YX5JJHAVCNFSM6AAAAABESEDECOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSOJSGM4DGOJSG4>
> .
> You are receiving this because you authored the thread.Message ID:
> ***@***.***>
>
|
Hi @EnricoMi if I submit a PR would you accept this? there's a good reason why this is not included? there's a similar API also for downloading job artifacts. request_url = f"{__gh_base_url__}/repos/{artifact_data['repo_name']}/releases/assets/{artifact_data['id']}"
headers = {
"Accept": "application/octet-stream",
"Authorization": f"Bearer {token}",
}
with open(destfile, "wb") as f:
r = requests.get(request_url, headers=headers, stream=True, timeout=5)
total_length = r.headers.get("content-length")
if total_length is None: # no content length header
f.write(r.content)
else:
for chunk in r.iter_content(chunk_size=4096):
f.write(chunk) the missing functionality is to be able to set this header
|
@nicola-lunghi yes, please, go ahead! |
I had a local change for |
Will do. |
Looks like there have been some changes to the repo, so will need to refactor my change to work with the changed code. |
First draft is ready here: #3060 |
Any comments on the PR? |
If someone is looking for a quick workaround for private repositories, this is what I came up with. def download_asset(asset: GitReleaseAsset) -> str:
"""Download a release asset.
Args:
asset: The release asset to download.
"""
headers = {"Accept": "application/octet-stream"}
(status, headers, _) = asset.requester.requestBlob("GET", asset.url, headers=headers)
if status != 302:
msg = f"Download failed expected 302 got {status}"
raise Exception(msg)
response = requests.get(headers["location"])
if response.status_code != 200:
msg = f"Download failed expected 200 got {response.status_code}"
raise Exception(msg)
return response.text |
The documentation says that I can download a release assets with:
Can I do it with this library?
The text was updated successfully, but these errors were encountered: