@@ -26,13 +26,17 @@ function Test-SHA256($filename) {
2626 return $true
2727}
2828
29- function Get-Blob ([string ]$token , [string ]$image , [string ]$digest , [string ]$registry = " registry.hub.docker.com" , $dist ) {
29+ function Get-Blob ([string ]$token , [string ]$image , [string ]$digest , [string ]$header = ' ' , [ string ] $ registry = " registry.hub.docker.com" , $dist ) {
3030 Write-Host " ==> Digest: $digest " - ForegroundColor Green
3131 $sha256 = $digest.split (' :' )[1 ]
3232 $prefix = $sha256.Substring (0 , 2 )
3333 New-Item - force - type Directory (Get-CachePath blobs/ sha256/ $prefix ) | out-null
3434 $distTemp = Get-CachePath " blobs/sha256/$prefix /$sha256 "
3535
36+ if (! $header ) {
37+ $header = [DockerImageSpec ]::layer
38+ }
39+
3640 if (Test-Path $distTemp ) {
3741 if (Test-SHA256 $distTemp ) {
3842 Write-Host " ==> File already exists, skip download" - ForegroundColor Green
@@ -47,7 +51,7 @@ function Get-Blob([string]$token, [string]$image, [string]$digest, [string]$regi
4751 $response = Invoke-WebRequest `
4852 - Authentication OAuth `
4953 - Token (ConvertTo-SecureString $token - Force - AsPlainText) `
50- - Headers @ {" Accept" = [ DockerImageSpec ]::layer } `
54+ - Headers @ {" Accept" = $header } `
5155 " https://$registry /v2/$image /blobs/$digest " `
5256 - PassThru `
5357 - OutFile $distTemp `
@@ -59,6 +63,8 @@ function Get-Blob([string]$token, [string]$image, [string]$digest, [string]$regi
5963
6064 $statusCode = $response.StatusCode
6165
66+ write-host $response
67+
6268 if (! $statusCode ) {
6369 Write-Host $_.Exception
6470
@@ -67,7 +73,7 @@ function Get-Blob([string]$token, [string]$image, [string]$digest, [string]$regi
6773 elseif ($statusCode -lt 400 -and $statusCode -gt 200 ) {
6874 $url = $response.Headers.Location
6975
70- # Write-Host "==> Redirect to $url" -ForegroundColor Magenta
76+ Write-Host " ==> Redirect to $url " - ForegroundColor Magenta
7177
7278 try {
7379 Invoke-WebRequest `
@@ -82,6 +88,28 @@ function Get-Blob([string]$token, [string]$image, [string]$digest, [string]$regi
8288 return $false
8389 }
8490 }
91+ elseif ($statusCode -eq 400 ) {
92+ try {
93+ $response = Invoke-WebRequest `
94+ - Authentication OAuth `
95+ - Token (ConvertTo-SecureString $token - Force - AsPlainText) `
96+ - Headers @ {" Accept" = $header } `
97+ " https://$registry /v2/$image /blobs/$digest " `
98+ - PassThru `
99+ - OutFile $distTemp `
100+ - UserAgent " Docker-Client/20.10.16 (Windows)"
101+ }
102+ catch {
103+ $response = $_.Exception.Response
104+
105+ write-host $response
106+
107+ $statusCode = $response.StatusCode
108+ Write-Host " ==> Get blob failed [ $statusCode ]" - ForegroundColor Red
109+
110+ return $false
111+ }
112+ }
85113 else {
86114 Write-Host " ==> Get blob failed [ $statusCode ]" - ForegroundColor Red
87115
0 commit comments