Dinesh Madhup
Dinesh Madhup

Reputation: 367

How to write data in Azure Blob storage programmatically?

I am using below PowerShell script to read JSON data using REST API call from source. Now I want to load the data of $Result to the Azure Blob Storage. Any idea please?

$Params = @{
 "URI" = 'https://3ea5e53b-817e-4c41-ae0b-c5afc1610f4e-bluemix.cloudant.com/test/_all_docs?include_docs=true'
}
 
$Result = Invoke-RestMethod @Params | ConvertTo-Json -Depth 9

Upvotes: 2

Views: 4194

Answers (1)

Jim Xu
Jim Xu

Reputation: 23111

Regarding the issue, you can use the following ways

  1. Save the JSON into one file then upload the file to Azure blob
$Params = @{
 "URI" = 'https://3ea5e53b-817e-4c41-ae0b-c5afc1610f4e-bluemix.cloudant.com/test/_all_docs?include_docs=true'
}
 
$Result = Invoke-RestMethod @Params | ConvertTo-Json -Depth 9
$Result | Out-File "D:\file.json"

$context=New-AzStorageContext -StorageAccountName "andyprivate" -StorageAccountKey ""

Set-AzStorageBlobContent -File "D:\file.json" `
  -Container "" `
  -Blob "file.json" `
  -Context $context `
  -StandardBlobTier Hot

enter image description here

  1. Directly upload to Azure blob
$Params = @{
 "URI" = 'https://3ea5e53b-817e-4c41-ae0b-c5afc1610f4e-bluemix.cloudant.com/test/_all_docs?include_docs=true'
}
 
$Result = Invoke-RestMethod @Params | ConvertTo-Json -Depth 9
Write-Host "the result is :"
$Result 

$context=New-AzStorageContext -StorageAccountName "andyprivate" -StorageAccountKey ""

$container=Get-AzStorageContainer -Name "input" -Context $context

$content = [system.Text.Encoding]::UTF8.GetBytes($Result)

$container.CloudBlobContainer.GetBlockBlobReference("my.json").UploadFromByteArray($content,0,$content.Length)

enter image description here enter image description here

Upvotes: 4

Related Questions