How to batch change the content type of Microsoft Azure blob storage (PowerShell) 


This PowerShell script sample shows how to batch change the content type of Microsoft Azure blob storage. 



In some cases, a lot of media files stored on blob are in different formats - mp4, image file, etc. Sometimes, all these files are set as one content type: application/octet-stream, which causes issues in media playback and progress bar. Many users would like to know how to set the appropriate Content-type for files stored on blob (like - video/mp4, video/ogg, video/webm)—not to do it manually for each file through blob interface. This script will help users easily batch change the content type of blob storage. It allows users to change content type of blob storage to any data type.


Step 1: From the Start Screen or the Start Menu, search for Windows Azure PowerShell. Right-click the Windows Azure PowerShell entry and select Run as Administrator.

Note: If Windows Azure PowerShell is not installed, see Getting Started with Windows Azure PowerShell Cmdlets for installation and configuration information.

Step 2: If you want to get a list of all cmdlet help topics, type the command Get-Help C:\Script\ ChangeAzureBlobContentType.ps1-Full to display the entire help file for this function, such as the syntax, parameters, or examples. This is shown in the following figure.


Example 1: Type C:\Script\ChangeAzureBlobContentType.ps1 -StorageAccountName "storageaccount" -ContainerName "pics" -BlobName "doc.txt" -ContentType "CustomType" command in the Windows PowerShell Console.

This example shows how to change the content type of 'doc.txt' to 'CustomType'.

Example 2: Type C:\Script\ChangeAzureBlobContentType.ps1 -StorageAccountName "storageaccount" -ContainerName "pics" -ContentType "CustomType" command in the Windows PowerShell Console.  If you don't specify a BlobName parameter, the script will batch change the content type of all blob items in the specified container and in all child containers.

This example shows how to batch change the content type of all blob items to "CustomType" in the "pics" container and in all child containers.

Example 3: Type C:\Script\ChangeAzureBlobContentType.ps1 -StorageAccountName "storageaccount" -ContainerName "pics" -ContentType "CustomType" -WhatIf command in the Windows PowerShell Console. 

It displays a message that describes the effect of the command, instead of executing the command.

Here are some code snippets for your reference.

Write-Verbose "Getting the container object named $ContainerName." 
$BlobContainer = $CloudBlobClient.GetContainerReference($ContainerName) 
Write-Verbose "Getting the blob object named $BlobName." 
$Blob = $BlobContainer.GetBlockBlobReference($BlobName) 
Write-Verbose "Changing content type of '$BlobName' to '$ContentType'." 
    $Blob.Properties.ContentType = $ContentType 
    Write-Host "Successfully changed content type of '$BlobName' to '$ContentType'."} 
    Write-Host "Failed to change content type of '$BlobName'." -ForegroundColor Red 


Windows PowerShell 3.0
Windows Azure PowerShell