[英]How to download a file in a branch from a Git repo using Azure DevOps REST Api?
[英]Trying to download a file from an Azure DevOps Git repo using Powershell
所以我在 Azure DevOps 中有一个 Git 回购,与 URL 类似
https://[$server]/tfs/[$company]/[$project]/_git/[$repoName]
我可以通过附加如下内容来访问该存储库中的单个文件:
?path=/[$folder]/[$fileName]
我正在尝试使用 Powershell 将此存储库中的特定文件下载到我计算机上的相应位置和文件名,如下所示:
$sourcePath = "https://[$server]/tfs/[$company]/[$project]/_git/[$repoName]?path=/[$folder]/[$fileName]&download=true"
$filePath = "C:\Documents\[$folder]\[$fileName]"
Invoke-RestMethod -Uri $sourcePath -Method Get -Headers @{Authorization=("Basic {0}" -f [$AuthInfo])} -OutFile $filePath
这样做不是用 repo 中的文件替换本地文件,而是用响应正文的内容替换本地文件的内容。 我觉得奇怪的是,我一直在做的所有谷歌搜索都说要这样做,尽管关于 Invoke-RestMethod 的 Microsoft 文章实际上解释了这就是-OutFile
的作用。
注意我也试过Invoke-WebRequest
....同样的事情。
那么如何将实际文件从 repo 下载到我想要的本地目标(或将本地目标文件的内容替换为 repo 文件的内容)?
另外,有没有办法指定从哪个分支获取文件? 也许我应该以某种方式使用 Git powershell 命令? 我所做的关于从 git 存储库下载的所有其他谷歌搜索都得出了关于 GitHub 的结果。
谢谢!
将 rest api 与以下模板一起使用:
GET https://{tfs_url}/{collection_name}/{project}/_apis/git/repositories/{repositoryId}/items?path={path}
我使用以下代码在构建管道中复制文件(使用System.Net.WebClient
):
$user = ""
$token = $env:SYSTEM_ACCESSTOKEN
$teamProject = $env:SYSTEM_TEAMPROJECT
$orgUrl = $env:SYSTEM_COLLECTIONURI
$repoName = "REPO_NAME"
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))
function InvokeGetFileRequest ($GitFilePath, $OutFilePath)
{
Write-Host "Download file" $GitFilePath "to" $OutFilePath
$uriGetFile = "$orgUrl/$teamProject/_apis/git/repositories/$repoName/items?scopePath=$GitFilePath&download=true&api-version=6.1-preview.1"
Write-Host "Url:" $uriGetFile
$wc = New-Object System.Net.WebClient
$wc.Headers["Authorization"] = "Basic {0}" -f $base64AuthInfo
$wc.Headers["Content-Type"] = "application/json";
$wc.DownloadFile($uriGetFile, $OutFilePath)
}
我需要这样做,但我在这里看到的没有用,所以我自己写了:
param(
[Parameter(Mandatory=$true)]
[string] $GitFilePath,
[Parameter(Mandatory=$true)]
[string] $OutFilePath,
[Parameter(Mandatory=$true)]
[string] $RepoName,
[string] $token,
[string] $orgUrl,
[string] $teamProject
)
if([String]::IsNullOrEmpty($token))
{
if($env:SYSTEM_ACCESSTOKEN -eq $null)
{
Write-Error "you must either pass the -token parameter or use the BUILD_TOKEN environment variable"
exit 1;
}
else
{
$token = $env:SYSTEM_ACCESSTOKEN;
}
}
if([string]::IsNullOrEmpty($teamProject)){
if($env:SYSTEM_TEAMPROJECT -eq $null)
{
Write-Error "you must either pass the -teampProject parameter or use the SYSTEM_TEAMPROJECT environment variable"
exit 1;
}
else
{
$teamProject = $env:SYSTEM_TEAMPROJECT
}
}
if([string]::IsNullOrEmpty($orgUrl)){
if($env:SYSTEM_COLLECTIONURI -eq $null)
{
Write-Error "you must either pass the -orgUrl parameter or use the SYSTEM_COLLECTIONURI environment variable"
exit 1;
}
else
{
$teamProject = $env:SYSTEM_COLLECTIONURI
}
}
# Base64-encodes the Personal Access Token (PAT) appropriately
$User=''
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $User,$token)));
$header = @{Authorization=("Basic {0}" -f $base64AuthInfo)};
#----------------------------------------------------------------------
Write-Host "Download file" $GitFilePath "to" $OutFilePath
$uriGetFile = "$orgUrl/$teamProject/_apis/git/repositories/$repoName/items?scopePath=$GitFilePath&download=true&api-version=6.1-preview.1"
Write-Host "Url:" $uriGetFile
$filecontent = Invoke-RestMethod -ContentType "application/json" -UseBasicParsing -Headers $header -Uri $uriGetFile
$filecontent | Out-File -Encoding utf8 $OutFilePath
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.