首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用az powershell进行Az acr导入?

如何使用az powershell进行Az acr导入?
EN

Stack Overflow用户
提问于 2020-05-22 10:36:47
回答 2查看 304关注 0票数 0

如果Az powershell没有它,那么使用REST Api的工作代码示例将会很有帮助。

这是我将要追求的道路,但如果有人有一个工作样本-请分享。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-05-23 04:42:01

所以我设法使用REST API触发了导入。下面是我的代码(使用服务主体登录):

代码语言:javascript
复制
function Get-AcrId($SubId, $RGName, $AcrName)
{
    "/subscriptions/$SubId/resourceGroups/$RGName/providers/Microsoft.ContainerRegistry/registries/$ACRName"
}

function Get-AzureAuthenticationToken(
    [Parameter(Mandatory)][String]$TenantID,
    [Parameter(Mandatory)][String]$ClientID,
    [Parameter(Mandatory)][String]$ClientSecret,
    [Parameter(Mandatory)][String]$ResourceAppIDUri)
{
    $tokenResponse = Invoke-RestMethod -Method Post -UseBasicParsing `
        -Uri "https://login.windows.net/$TenantID/oauth2/token" `
        -Body @{
        resource      = $ResourceAppIDUri
        client_id     = $ClientID
        grant_type    = 'client_credentials'
        client_secret = $ClientSecret
    } -ContentType 'application/x-www-form-urlencoded'

    Write-Verbose "Access token type is $($tokenResponse.token_type), expires $($tokenResponse.expires_on)"
    $tokenResponse.access_token
}

function Import-DockerImage(
    [Parameter(Mandatory)]$SourceSubId,
    [Parameter(Mandatory)]$SourceRGName,
    [Parameter(Mandatory)]$SourceACRName,
    [Parameter(Mandatory)]$TargetSubId,
    [Parameter(Mandatory)]$TargetRGName,
    [Parameter(Mandatory)]$TargetACRName,
    [Parameter(Mandatory)]$ImageName,
    [Parameter(Mandatory)]$ImageTag
)
{
    $AzContext = Get-AzContext
    if (!$AzContext)
    {
        throw "No Az context is found."
    }
    $TenantId = $AzContext.Tenant.Id
    $ClientId = $AzContext.Account.Id
    $ClientSecret = $AzContext.Account.ExtendedProperties.ServicePrincipalSecret
    $token = Get-AzureAuthenticationToken -TenantID $TenantId -ClientID $ClientId -ClientSecret $ClientSecret -ResourceAppIDUri "https://management.core.windows.net/"

    $url = "https://management.azure.com$(Get-AcrId $TargetSubId $TargetRGName $TargetACRName)/importImage?api-version=2019-05-01"
    $body = @{
        source     = @{
            resourceId  = Get-AcrId $SourceSubId $SourceRGName $SourceACRName
            sourceImage = "${ImageName}:$ImageTag"
        }
        targetTags = @(
            "${ImageName}:$ImageTag"
        )
        mode       = "NoForce"
    } | ConvertTo-Json -Depth 99
    $headers = @{
        "Authorization" = "Bearer $token"
        "Content-Type"  = "application/json" 
    }
    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
    $r = Invoke-WebRequest $url -Method Post -Headers $headers -Body $body

    $headers.Remove('Content-Type')
    while ($r.StatusCode -eq 202)
    {
        $RetryAfter = $r.Headers.'Retry-After'
        $Location = $r.Headers.Location
        Start-Sleep -Seconds $RetryAfter
        $r = Invoke-WebRequest $Location -Headers $headers
    }
票数 0
EN

Stack Overflow用户

发布于 2020-05-22 11:15:51

通过Azure PowerShell将图像导入ACR是不可能的,但REST API是存在的。看一看Import Image REST API

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61946941

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档