问题描述
创建一个PowerShell脚本的Azure Function,触发方式为 Storage Queue。但执行函数结果一直失败 (Failed).
错误消息为:
Executed 'Functions.QueueTrigger1' (Failed, Id=..., Duration=30ms) The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.
Funciton 代码:
# Input bindings are passed in via param block.
param([string] $QueueItem, $TriggerMetadata)
# Write out the queue message and metadata to the information log.
Write-Host "PowerShell queue trigger function processed work item: $QueueItem"
Write-Host "Queue item expiration time: $($TriggerMetadata.ExpirationTime)"
Write-Host "Queue item insertion time: $($TriggerMetadata.InsertionTime)"
Write-Host "Queue item next visible time: $($TriggerMetadata.NextVisibleTime)"
Write-Host "ID: $($TriggerMetadata.Id)"
Write-Host "Pop receipt: $($TriggerMetadata.PopReceipt)"
Write-Host "Dequeue count: $($TriggerMetadata.DequeueCount)"
通过向Storage Queue中添加消息时候触发:
az storage message put --content "This is a function test message"
问题分析
根据错误消息,Function (Storage Queue)触发的消息需要 Base64编码,如发送 "This is a function test message" 这段消息,需要在发送消息时候转换为base64编码。
在CMD中调用PowerShell进行类型转换:
powershell "[convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes(\"This is a function test message\"))"
在Powershell中直接转换:
[convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes("This is a function test message")
所以,修改后的向Storage Queue中添加消息的命令为:
$queuemsg = [convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes("This is a function test message"))
az storage message put --content $queuemsg
参考资料
适用于 Azure Functions 的 Azure 队列存储触发器: https://docs.azure.cn/zh-cn/azure-functions/functions-bindings-storage-queue-trigger?tabs=powershell#encoding
当在复杂的环境中面临问题,格物之道需:浊而静之徐清,安以动之徐生。 云中,恰是如此!
分类: 【Azure 应用服务】
标签: App Service, Azure Developer, Azure Function, The input is not a valid Base-64 string, [convert]::ToBase64String