Created
February 12, 2026 21:52
-
-
Save nexocentric/99e9a023acbf1c9de2538995845e8cbf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Requires: Az.Accounts, Az.Storage | |
| # --- Inputs --- | |
| $SubscriptionId = "<sub-id>" # optional | |
| $ResourceGroupName = "<rg-name>" | |
| $StorageAccountName = "<storage-account-name>" # single SA target | |
| # Local root that contains per-container subfolders: | |
| # C:\uploads\storage\container-a\... | |
| # C:\uploads\storage\logs-2026-02\... | |
| $SourceRoot = "C:\uploads\storage" | |
| # Container filters (choose one style) | |
| $ContainerNames = @("container-a","container-b") # exact list (optional) | |
| $ContainerPatterns = @("logs-*","exports-*") # wildcard list (optional) | |
| $Overwrite = $true | |
| $ContentTypeAuto = $true # set basic content-type guessing (optional) | |
| # --- Login / context --- | |
| Connect-AzAccount | Out-Null | |
| if ($SubscriptionId -and $SubscriptionId -ne "<sub-id>") { | |
| Set-AzContext -SubscriptionId $SubscriptionId | Out-Null | |
| } | |
| # --- Filter to ONE storage account --- | |
| $sa = Get-AzStorageAccount -ResourceGroupName $ResourceGroupName | | |
| Where-Object StorageAccountName -EQ $StorageAccountName | | |
| Select-Object -First 1 | |
| if (-not $sa) { throw "Storage account '$StorageAccountName' not found in RG '$ResourceGroupName'." } | |
| $ctx = $sa.Context | |
| # --- List containers and filter to the set you want --- | |
| $containers = Get-AzStorageContainer -Context $ctx | |
| if ($ContainerNames -and $ContainerNames.Count -gt 0) { | |
| $containers = $containers | Where-Object { $ContainerNames -contains $_.Name } | |
| } | |
| if ($ContainerPatterns -and $ContainerPatterns.Count -gt 0) { | |
| $containers = $containers | Where-Object { | |
| $name = $_.Name | |
| $ContainerPatterns | Where-Object { $name -like $_ } | Select-Object -First 1 | |
| } | |
| } | |
| if (-not $containers) { throw "No containers matched the provided filters." } | |
| # --- Upload --- | |
| foreach ($c in $containers) { | |
| $containerName = $c.Name | |
| $localContainerPath = Join-Path $SourceRoot $containerName | |
| if (-not (Test-Path $localContainerPath)) { | |
| Write-Host "==> Container: $containerName (SKIP - no local folder '$localContainerPath')" | |
| continue | |
| } | |
| Write-Host "==> Container: $containerName" | |
| $files = Get-ChildItem -Path $localContainerPath -Recurse -File | |
| foreach ($f in $files) { | |
| # Build blob name relative to the container folder, using forward slashes | |
| $relative = $f.FullName.Substring($localContainerPath.Length).TrimStart('\','/') | |
| $blobName = ($relative -replace '\\','/') | |
| # Optional: best-effort content-type | |
| $props = @{} | |
| if ($ContentTypeAuto) { | |
| switch -Regex ($f.Extension.ToLowerInvariant()) { | |
| '\.json' { $props.ContentType = 'application/json' } | |
| '\.txt' { $props.ContentType = 'text/plain' } | |
| '\.csv' { $props.ContentType = 'text/csv' } | |
| '\.htm(l)?' { $props.ContentType = 'text/html' } | |
| '\.xml' { $props.ContentType = 'application/xml' } | |
| '\.png' { $props.ContentType = 'image/png' } | |
| '\.jpe?g'{ $props.ContentType = 'image/jpeg' } | |
| '\.pdf' { $props.ContentType = 'application/pdf' } | |
| default { } | |
| } | |
| } | |
| Write-Host " Uploading: $blobName" | |
| Set-AzStorageBlobContent ` | |
| -Container $containerName ` | |
| -File $f.FullName ` | |
| -Blob $blobName ` | |
| -Context $ctx ` | |
| -Force:$Overwrite ` | |
| @props | Out-Null | |
| } | |
| } | |
| Write-Host "Done. Uploaded from: $SourceRoot" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment