简体   繁体   English

在 Python 中下载 Azure Devops 工件

[英]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 eg 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 eg

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.但是,它不是下载工件,而是下载 HTML 页面。 The same link does work in a web browser.相同的链接在 web 浏览器中有效。

How can I download the artifacts like I would in YAML?如何像在 YAML 中一样下载工件?

We have API version 6.0 for python package download for Azure DevOps Artifacts:我们有 API 版本 6.0 用于 python package 下载 Z3A580F142203677F0BCOpsif89

Below is the API, this can be used in any UI下面是 API,可以在任何 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您可以从 Azure Docs 查看所有参数

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.在 Windows 中,您可以通过滥用注册表 + Microsoft Edge 来使用 Powershell 来执行此操作(我猜可以从 Python 调用)。

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

Import requests in your script and use this API of type GET:在脚本中导入请求并使用 GET 类型的 API:

https://dev.azure.com/{organization}/{project}/_apis/build/builds/{build_id}/artifacts?artifactName={artifactName}&api-version=6.0&%24format=zip 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)工件名称是您在构建管道的发布构建任务中提供的名称(默认为 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.然后,您将在当前工作目录中获得已发布工件的 zip 文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM