简体   繁体   English

使用 PowerShell 脚本从 Azure Blob 存储读取 JSON 文件并写回 Blob 存储中的另一个文件

[英]Read JSON file from Azure Blob Storage using PowerShell script and write back to another file in blob storage

I have following JSON file (product.json) stored in Azure Blob storage.我有以下 JSON 文件(product.json)存储在 Azure Blob 存储中。 Is it possible to write PowerShell script to read this file from blob storage make some changes and write back to another file.是否可以编写 PowerShell 脚本从 blob 存储读取此文件进行一些更改并写回另一个文件。 The output file I would like where following changes should occur: output 文件我想在哪里发生以下更改:

  1. Replace all "_id" with "id"将所有“_id”替换为“id”
  2. Remove all "_rev" and their values.删除所有“_rev”及其值。

Product.json产品.json

[
  {
    "_id": "9f4da9d6babeb9d411c896baa68c94c8",
    "_rev": "1-4259271795225df18768ab68baacc96c",
    "account_id": 692278,
    "limit": 10000,
    "products": [
      "Commodity",
      "InvestmentStock"
    ]
  },
  {
    "_id": "cc4b59f585b8556a2bedca78294a0797",
    "_rev": "1-410e479257faba0457bd9b4816c4dc95",
    "account_id": 328304,
    "limit": 10000,
    "products": [
      "Derivatives",
      "InvestmentStock",
      "CurrencyService"
    ]
  },
  {
    "_id": "d7e2a72963cff2760514ff772969ffe0",
    "_rev": "1-2ec6e2679eae13b76410c93f49c14c4a",
    "account_id": 674364,
    "limit": 10000,
    "products": [
      "InvestmentStock"
    ]
  }
]

The outputfile.json should be as follows: outputfile.json应该如下:

[
  {
    "id": "9f4da9d6babeb9d411c896baa68c94c8",
    "account_id": 692278,
    "limit": 10000,
    "products": [
      "Commodity",
      "InvestmentStock"
    ]
  },
  {
    "id": "cc4b59f585b8556a2bedca78294a0797",
    "account_id": 328304,
    "limit": 10000,
    "products": [
      "Derivatives",
      "InvestmentStock",
      "CurrencyService"
    ]
  },
  {
    "id": "d7e2a72963cff2760514ff772969ffe0",
    "account_id": 674364,
    "limit": 10000,
    "products": [
      "InvestmentStock"
    ]
  }
]

Not the most elegant way, but this should do the trick:不是最优雅的方式,但这应该可以解决问题:

$accountName = "account-name"
$accountKey = "account-key"
$containerName = "container-name"
$blobName = "Product.json"
$outputBlobName = "Output.json"

$ctx = New-AzStorageContext -StorageAccountName $accountName -StorageAccountKey $accountKey

#Read blob and save it to local file
Get-AzStorageBlobContent -Blob $blobName -Container $containerName -Destination "Product.json" -Context $ctx

#Read local file and get JSON object
$jsonContent = Get-Content -Raw -Path "Product.json" | ConvertFrom-Json

#Loop through json content and manipulate it
For ($i=0; $i -lt $jsonContent.Length; $i++) {
    $jsonContent[$i] | Add-Member -NotePropertyName "id" -NotePropertyValue $jsonContent[$i]._id
    $jsonContent[$i].PsObject.Properties.Remove("_id")
    $jsonContent[$i].PsObject.Properties.Remove("_rev")
}
$jsonContent

#Write content back to local disk
$jsonContent | ConvertTo-Json | Set-Content -Path $outputBlobName

#Upload into Azure Storage
$properties = @{"ContentType" = "application/json"}
Set-AzStorageBlobContent -File $outputBlobName -Container $containerName -Blob $outputBlobName -Properties $properties -Context $ctx

If you want to store the file in memory, you can use the DownloadText() method to download the content into memory.如果要将文件存储在 memory 中,可以使用DownloadText()方法将内容下载到 memory 中。

The sample code:示例代码:

$accountName = "xxx"
$accountKey = "xxx"
$containerName = "xxx"
$blobName = "Product.json"
$outputBlobName = "Output.json"    

$context = New-AzStorageContext -StorageAccountName $accountName -StorageAccountKey $accountKey
$container_client = Get-AzStorageContainer -Name $containerName -Context $context
$source_blob_client = $container_client.CloudBlobContainer.GetBlockBlobReference($blobName)

#download the blob as text into memory
$download_file = $source_blob_client.DownloadText()

$jsonContent = $download_file | ConvertFrom-Json

#Loop through json content and manipulate it
For ($i=0; $i -lt $jsonContent.Length; $i++) {
    $jsonContent[$i] | Add-Member -NotePropertyName "id" -NotePropertyValue $jsonContent[$i]._id
    $jsonContent[$i].PsObject.Properties.Remove("_id")
    $jsonContent[$i].PsObject.Properties.Remove("_rev")
}

$replaced_json = $jsonContent | ConvertTo-Json

#upload the json file
$dest_blob_client =  $container_client.CloudBlobContainer.GetBlockBlobReference($outputBlobName)
$dest_blob_client.Properties.ContentType = "application/json"
$dest_blob_client.UploadText($replaced_json)

Write-Output("**completed**")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 带有Powershell脚本的Azure WebJob可将文件上传到Azure Blob存储 - Azure WebJob with Powershell Script to upload file to Azure Blob storage 写入 Azure 存储 Blob (PowerShell) - Write to Azure Storage Blob (PowerShell) 使用 Azure 模块在 PowerShell 中使用 Azure 函数将文件上传到 blob 存储 - upload file to blob storage with Azure functions in PowerShell using Azure module Powershell:将文件从 Azure 文件共享复制到 Blob 存储帐户 - Powershell: Copy file from Azure File Share to Blob Storage account 如何命名从Powershell Azure函数在Blob存储中创建的文件? - How to name the file created in blob storage from a Powershell Azure Function? Azure Powershell-如何使用“仅写”共享访问签名(SAS)将文件上传到Blob存储? - Azure Powershell - How to upload file to Blob Storage using “write-only” shared access signature (SAS)? 如何使用Powershell从Azure存储下载blob文件并将其保存到FTP服务器? - How to download a blob file from Azure Storage and save it to an FTP server using Powershell? 从Automation PowerShell中的Azure Blob存储读取XML - Read XML from Azure Blob storage in Automation PowerShell PowerShell:从Azure Blob存储运行Python脚本 - PowerShell: run Python script from Azure blob storage 如何使用 PowerShell 或 python 脚本读取然后编辑或附加存储在 Azure Blob 存储中的 Excel 文件(列和行) - How to read then edit or append an Excel Files (columns and rows ) stored in Azure Blob Storage using PowerShell or python script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM