0

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.

2
  • Isn't the result you're getting from Invoke-RestMethod already in JSON format? If so, remove the pipe to ConvertTo-Json Commented Dec 8, 2022 at 14:41
  • @Theo I tried that too but for some reason not being able to set content-type to application/json Commented Dec 8, 2022 at 17:55

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

I'm reading the data into the variable $Result. How do I use this with -File parameter. I guess in your example the -OutFile set to a local drive. But, in my case I would need to read the file from a variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.