使用PowerShell将JSON文件直接保存到Blob存储

时间:2019-09-29 17:10:26

标签: azure-storage-blobs powershell-3.0 azure-powershell blobstorage

我想将API调用中的JSON文件直接保存到Blob存储,而不是将文件保存到本地文件夹并将其传输到Blob存储。

我下面的代码调用一个API,然后将其保存到本地文件夹中。是否可以通过PowerShell将其直接保存到Blob存储中

$logPath = 'C:\Dev\Template\Test'

$access_token ="Access_Token"

$URI =  "https://XXXXX"
$headers = @{“authorization” = “Bearer $access_token”} 
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$result = Invoke-RestMethod -Uri $URI -Headers $headers -ContentType $ContentType |ConvertTo-Json
$Result|ConvertFrom-Json| Select -ExpandProperty Forms| Out-File "$logPath\$report_iu.json"

1 个答案:

答案 0 :(得分:0)

您可以将结果转换为json,然后使用UploadText将其上传到Azure存储。

#get the json
$result = Invoke-RestMethod -Uri $URI -Headers $headers -ContentType $ContentType |ConvertTo-Json

#load the blob storage module
[System.Reflection.Assembly]::LoadFile("D:\FIle\Microsoft.WindowsAzure.Storage.dll")
$connstr="DefaultEndpointsProtocol=https;AccountNamexxx;AccountKey=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx;EndpointSuffix=core.windows.net"
$storageAccount = [microsoft.windowsazure.storage.cloudstorageaccount]::parse($connstr)
$blobclient=$storageAccount.CreateCloudBlobClient()
$cloudBlobContainer = $blobclient.GetContainerReference("mycontainer")
$cloudBlockBlob = $cloudBlobContainer.GetBlockBlobReference("my.json")

#upload json to blob storage
$cloudBlockBlob.UploadText($result)