【问题标题】:How to convert image to Base64 string, and convert back to image, save to azure blob如何将图像转换为 Base64 字符串,并转换回图像,保存到 azure blob
【发布时间】:2021-08-18 19:06:48
【问题描述】:

希望我描述的正确,而且可能非常简单

尝试将图像作为 base64 字符串在 json 有效负载中传递给用 Powershell 编写的 azure 函数,同时旨在将其最终作为结构化 azure blob 存储中的图像。

将本地文件编码为Base64:

$content = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes([System.IO.File]::ReadAllText("$fileName")))
        

打包、发送和接收 json 负载没有问题。

在 Azure 函数中,http 触发器

解码base64

$image = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($base64Content))
        

从那里,输出到 blob:

Push-OutputBinding -Name outputBlob -Value $ImageBytes

输出的标准绑定:

{
   "name": "outputBlob",
   "path": "container/{filename}",
   "connection": "connectionName",
   "direction": "out",
   "type": "blob"
}

生成的 blob 不是图像,假设它是解码。

提前谢谢..

【问题讨论】:

  • 您遇到了什么问题?请编辑您的问题并提供该信息。还有$ImageBytes从哪里来?是不是错字,应该是$bytes
  • 能否详细描述您的问题?

标签: azure powershell base64 blob


【解决方案1】:

关于问题,请参考以下代码

功能代码

using namespace System.Net

# Input bindings are passed in via param block.
param($Request, $TriggerMetadata)

# Write to the Azure Functions log stream.
Write-Host "PowerShell HTTP trigger function processed a request."

$image=$Request.Body.Image

#read base64 image as bytes
$imageBytes=[System.Convert]::FromBase64String($image)

Push-OutputBinding -Name outputBlob -Value  $imageBytes

输出的标准绑定:

{
   "name": "outputBlob",
   "path": "outcontainer/{filename}",
   "connection": "connectionName",
   "direction": "out",
   "type": "blob"
}

用于发送请求的脚本

$fileName="faces.jpg"
$content=Get-Content -Path $fileName -Encoding Byte
$image=[System.Convert]::ToBase64String($content)

$body= @{"fileName"=$fileName;"image"=$image}|convertto-json

Invoke-WebRequest -Method Post -Uri "<url>" -Body $body -ContentType "application/json" -UseBasicParsing

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-11
    • 2015-04-22
    • 1970-01-01
    • 2018-09-07
    • 2014-07-21
    • 2011-06-17
    • 1970-01-01
    • 2019-10-03
    相关资源
    最近更新 更多