Forum Discussion
How to change blob content type using powershell script
Hi,
I'm trying to use the following script to change the content type of multiple blob storage files. I would like to add multiple blob.names so the script can update the content type of select blobs.
When I run a test (script below) to see if a single blob is updated, the code just hangs and takes a long time. Any ideas please?
#Login-AzureAccount
Connect-AzAccount
# Set the tenant, subscription and environment for use in the rest of
Set-AzContext -SubscriptionName $subscriptionName
#----View application/octet-stream Files ------------------------------
$ContentType = "application/octet-stream"
$storageKey = (Get-AzStorageAccountKey -ResourceGroupName $ResourceGroupName -Name $acct).Value[0]
$ctx = New-AzStorageContext -StorageAccountName $acct -StorageAccountKey $storageKey;
$blobs = Get-AzStorageBlob $BlobName -Container $Container -Context $ctx #-MaxCount 10
foreach ($blob in $blobs)
{
if($blob.Name -like "*102398*"-and ($blob.ContentType -like "*text*"))
{
$CloudBlockBlob = [Microsoft.Azure.Storage.Blob.CloudBlockBlob] $Blob.ICloudBlob
$CloudBlockBlob.FetchAttributes()
Write-Host $blob.Name
$CloudBlockBlob.Properties.ContentType = $ContentType
$CloudBlockBlob.SetProperties()
}
}
1 Reply
- federicodlsCopper ContributorHi TWest1150.
I used your post to create my own script and wrote all in one line. Of course, you have to create the Context as you have done.
Get-AzStorageBlob -Container $containerName -Blob "*" -Context $context | foreach { $_.ICloudBlob.Properties.ContentType = 'text/html'; $_.ICloudBlob.SetProperties() }
I ran this from a Cloud Shell window in the Azure Portal against 6000 files and finished in just a few minutes.
You asked this far ago, but this is one of the first results searching and hope this helps someone else.
Regards.