2

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?

3 Answers 3

3

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.

1
  • I was just missing the auth=("", PAT) bit. Thank you!
    – LuisCien
    Commented Oct 24, 2022 at 12:35
0

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

1
  • Thats the incorrect API, OP was asking for build artifacts. Then again, the question wasnt clear from OP.
    – KevinLee
    Commented Jan 24, 2022 at 11:26
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.