Skip to content

Instantly share code, notes, and snippets.

@bcnzer
Created November 27, 2017 18:52
Show Gist options
  • Select an option

  • Save bcnzer/aa97b3399601b61b85c81ab3f8c89876 to your computer and use it in GitHub Desktop.

Select an option

Save bcnzer/aa97b3399601b61b85c81ab3f8c89876 to your computer and use it in GitHub Desktop.
Iterates through a collection of blobs in a container and fixes/sets the Content Type for types that AzCopy /SetContentType either misses or messes up
# The File Copy step (which uses AzCopy) has /SetContentType but it ignores a bunch of types and messes
# up the content type of others. This script fixes the common ones.
$StorageAccountName = "<ENTER YOUR STORAGE ACCOUNT NAME HERE>" # i.e. WolfTrackerStorage
# If you're using VSTS I would strongly suggest using Key Vault to store and retrieve the key. Keep secrets out of your code!
$StorageAccountKey = "<ENTER YOUR STORAGE KEY FROM THE PORTAL>"
$ContainerName = "<NAME OF THE BLOB CONTAINER>" # i.e. wolfpics
$Context = New-AzureStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey
$Blobs = Get-AzureStorageBlob -Context $Context -Container $ContainerName
foreach ($Blob in $Blobs)
{
$Extn = [IO.Path]::GetExtension($Blob.Name)
$ContentType = ""
# For some reason it sets .js to "application/x-javascript". If there are any other wierd types of content
# types missing altogether, add them below
switch ($Extn) {
".json" { $ContentType = "application/json" }
".js" { $ContentType = "application/javascript" }
".svg" { $ContentType = "image/svg+xml" }
Default { $ContentType = "" }
}
if ($ContentType -ne "") {
$CloudBlockBlob = [Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob] $Blob.ICloudBlob
$CloudBlockBlob.Properties.ContentType = $ContentType
$CloudBlockBlob.SetProperties()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment