paone
paone

Reputation: 937

powershell upload blob to azure blob storage set content type

I am making a ReST api call and uploading the JSON payload to azure blob storage using the below powershell.

PoSh:

        $Params = @{"URI" = 'https://myapiexample.com/api/data/'}
         
        $Result = Invoke-RestMethod @Params | ConvertTo-Json
        
        $context=New-AzStorageContext -StorageAccountName "mystorage" -StorageAccountKey ""
        
        $container=Get-AzStorageContainer -Name "input" -Context $context
        
        $content = [system.Text.Encoding]::UTF8.GetBytes($Result)
        $blobpath = "azblob/mydata.json"
        
        $container.CloudBlobContainer.GetBlockBlobReference($blobpath).UploadFromByteArray($content,0,$content.Length)
    
    $container.CloudBlobContainer.GetBlockBlobReference($blobpath).Properties.ContentType = "application/json"

$container.CloudBlobContainer.GetBlockBlobReference($blobpath).SetProperties()

I notice that when the blob is stored in azure blob storage the content-type is application/octet-stream whereas I would want it to be application/json. The code I use is not working as expected.

Upvotes: 0

Views: 1300

Answers (1)

SwethaKandikonda
SwethaKandikonda

Reputation: 8234

After reproducing from my end, I could able to achieve your requirement using Set-AzStorageBlobContent. Below is the complete code that is working for me that sets the content type of the blob.

$url = "<YOUR_URL>"
$dest = "samplex.json"
Invoke-WebRequest -Uri $url -OutFile $dest
$context = New-AzStorageContext -StorageAccountName "<YOUR_ACCOUNT_NAME>" -StorageAccountKey "<YOUR_ACCOUNT_KEY>"
Set-AzStorageBlobContent -Context $context -Container "containers" -Blob "mydata.json" -File $dest -Properties @{"ContentType" = "application/json"};

RESULTS:

enter image description here

enter image description here

Upvotes: 1

Related Questions