craigB
craigB

Reputation: 421

Download Azure Devops artifact in Python

I am able to use Azure Rest API in Python (https://github.com/microsoft/azure-devops-python-api) to get a download URL for an Artifact e.g.

artifacts = build_client.get_artifacts(project_name, build_id)

I then want to download them all with something like

for artifact in artifacts:
    urllib.request.urlretrieve(artifact.resource.download_url, artifactDownloadPath + artifact.name)

However rather than downloading the artifact it downloads the HTML page. The same link does work in a web browser.

How can I download the artifacts like I would in YAML?

Upvotes: 2

Views: 2524

Answers (3)

KevinLee
KevinLee

Reputation: 324

Import requests in your script and use this API of type GET:

https://dev.azure.com/{organization}/{project}/_apis/build/builds/{build_id}/artifacts?artifactName={artifactName}&api-version=6.0&%24format=zip

The artifact name is the name you provide in the publish build task in your build pipeline (by default it is drop)

Then retrieve the contents and create the file locally.

resp = requests.get(api, auth=("", PAT))
with open(f"{artifact_name}.zip", "wb") as f:
 f.write(resp.content)

You'll then get a zip file of the published artifacts in the current working directory.

Upvotes: 3

craigB
craigB

Reputation: 421

In Windows you can use Powershell to do this (which I guess can be called from Python if wanted) by abusing the registry + Microsoft Edge.

taskkill > $null /im msedge.exe /f /FI "STATUS eq RUNNING" 
Set-ItemProperty -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" -Name "{374DE290-123F-4565-9164-39C4925E467B}" -Value "C:\MyDownloadDirectory\"        
start $artifact.resource.downloadUrl

Upvotes: 0

SaiKarri-MT
SaiKarri-MT

Reputation: 1301

We have API version 6.0 for python package download for Azure DevOps Artifacts:

Below is the API, this can be used in any UI

GET https://pkgs.dev.azure.com/{organization}/{project}/_apis/packaging/feeds/{feedId}/pypi/packages/{packageName}/versions/{packageVersion}/{fileName}/content?api-version=6.0-preview.1

You can check all the parameter from Azure Docs

Upvotes: 0

Related Questions