Skip to content

Commit

Permalink
Custom Headers for Download-File (#3170)
Browse files Browse the repository at this point in the history
In our pipelines we have the need to authenticate using Oauth2 since we
have a private storage container, we are using your Download-File in the
scripts and I do not want to use a SAS token. I did not find any other
way than this, perhaps you have another idea that doesn't need a commit?
:)

```
$context = New-BcAuthContext -clientID "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" `
                             -tenantID "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" `
                             -clientSecret "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" `
                             -scopes "https://storage.azure.com/.default"

$headers = @{ 
    "Authorization" = "Bearer $($context.accessToken)"
    "x-ms-version" = "2017-11-09"
}

Download-File -sourceUrl "https://storage.blob.core.windows.net/container/file.file" `
              -destinationFile "c:\temp\file.file" `
              -headers $headers
```

---------

Co-authored-by: Mathias Stjernfelt <[email protected]>
  • Loading branch information
mstjernfelt and Mathias Stjernfelt authored Aug 31, 2023
1 parent 32c1f54 commit ab43bdf
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions Common/Download-File.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
Url from which the file will get downloaded
.Parameter destinationFile
Destinatin for the downloaded file
.Parameter Headers
Specify a custom header for the request
.Parameter dontOverwrite
Specify dontOverwrite if you want top skip downloading if the file already exists
.Parameter timeout
Expand All @@ -20,6 +22,7 @@ function Download-File {
[string] $sourceUrl,
[Parameter(Mandatory=$true)]
[string] $destinationFile,
[hashtable] $headers = @{"UserAgent" = "BcContainerHelper $bcContainerHelperVersion" },
[switch] $dontOverwrite,
[int] $timeout = 100
)
Expand Down Expand Up @@ -79,7 +82,7 @@ try {
$sourceUrl = ReplaceCDN -sourceUrl $sourceUrl
}
try {
DownloadFileLow -sourceUrl $sourceUrl -destinationFile $destinationFile -dontOverwrite:$dontOverwrite -timeout $timeout
DownloadFileLow -sourceUrl $sourceUrl -destinationFile $destinationFile -dontOverwrite:$dontOverwrite -timeout $timeout -headers $headers
}
catch {
try {
Expand All @@ -92,7 +95,7 @@ try {
Write-Host "Could not download from CDN..., retrying from blob storage in $waittime seconds..."
}
Start-Sleep -Seconds $waittime
DownloadFileLow -sourceUrl $newSourceUrl -destinationFile $destinationFile -dontOverwrite:$dontOverwrite -timeout $timeout
DownloadFileLow -sourceUrl $newSourceUrl -destinationFile $destinationFile -dontOverwrite:$dontOverwrite -timeout $timeout -headers $headers
}
catch {
throw (GetExtendedErrorMessage $_)
Expand Down

0 comments on commit ab43bdf

Please sign in to comment.