【问题标题】:Iterating through text files in blob storage, using Azure functions使用 Azure 函数迭代 Blob 存储中的文本文件
【发布时间】:2022-01-12 20:31:03
【问题描述】:

这里是 Azure 的新手。我最近接管了一个项目的大修,其中使用 Visual Basic 提取非结构化文本文件中的数据并将其插入到数据库表中。

长话短说,这个 VB 脚本有很多问题,所以我正在使用 Powershell 重写它。它不是很复杂,基本上只是逐行检查文件并使用不同的开始和停止信息提取数据,下面是一个小例子:

$fileName = ""
$fileContents = ""
$filePath = Get-ChildItem -Path C:\share\Batch -Exclude csv

for ($i = 0; $i -lt $filePath.Count; $i++) {
    $fileName = $filePath[$i].Name
    $fileContents = Get-Content $filePath[$i].FullName
    
    getTempLog $fileContents
    getAiringLog $fileContents
    getSteamLog $fileContents
    getProductionSummary $fileContents
}

function getTempLog {
    param ($fileContents)

        $tempLogStart = 0

        $fileContents | foreach-object {
         # end condition
        if ([int]$TempLogStart -eq 1 -and $_ -like "Genomluftning Flöde (m3/h) log:*") {
            break
        }

        #output tempLog data
        if ([int]$TempLogStart -eq 1) {
            Write-Host $_.Substring(0, 14) #this should go into database
            Write-Host $_.Substring(24, 4)
        }

        # start condition
        if ($_ -like "Temperature log:*") {
            $TempLogStart = 1
        }
    }
    
}

当然,当决定将此解决方案移至云中并将文件上传到 Azure Blob 存储时,事情变得更加复杂。

使用 Blob 存储作为输入创建 Azure 函数并不是什么大问题,但尝试以与我在本地文件时相同的方式读取 Blob,即

$inputBlob | foreach-object {

}

效果不佳,因为似乎 blob 输入变量被视为一个长连续字符串。我是不是完全走错了路?

【问题讨论】:

  • 如何初始化$inputBlob?在您的代码中,$fileContents 包含一个文本行数组,因为 Get-Content 将文件内容解析为单独的文本行。您可能需要对您的 blob 内容执行相同的操作。
  • Anatoli Beliaev - 目前我已经设置了一个 Azure 函数,它将 blob 作为字符串输入变量。我只是尝试在其上使用 foreach-object 方法。 Get-AzStorageBlobContent 在我的脑海中,但该命令也想下载 blob。我想读取 blob 并提取数据并在不下载的情况下发送。
  • 好的,一个简单的 $inputblob.Split([Environment]::NewLine) 对我有用。

标签: azure azure-functions azure-blob-storage azure-powershell


【解决方案1】:

正如@David Diamant 在评论部分所建议的那样,要使用 PowerShell 遍历 blob 存储中的文本文件,我们可以使用以下命令:

$inputblob.Split([Environment]::NewLine)

【讨论】:

    猜你喜欢
    • 2018-08-12
    • 2021-07-09
    • 1970-01-01
    • 2017-11-02
    • 2018-10-30
    • 2019-09-21
    • 2019-11-16
    • 2015-06-20
    相关资源
    最近更新 更多