2024-07-26
We wanted to upload attachments from an email into an Azure Blob Storage Account. This should’ve been a 5 minute adventure, instead this took “a while” over multiple days.
When you create something from the “Logic app designer” on the portal, you get something that looks like the following (only specific fields included for brevity),
{
"Create_blob_(V2)": {
"inputs": {
"body": "@{base64ToBinary(item()?['contentBytes'])}",
"headers": {
"Content-Type": "@{item()?['contentType']}",
"ReadFileMetadataFromServer": true
},
"queries": {
"folderPath": "/attachments",
"name": "@{item()?['name']}",
}
}
}
}
Now, per the “official documentation”, the “body” is supposed to be a “binary” type. So the above should be “correct”, but only for the logic app to mangle the output whenever the content type was not any “plain text without special characters”.
This was escalated as a “images and PDFs are not working”, but the underlying cause would be any binary file.
There have been multiple open tickets I can find on the internet which more-or-less indicate the same issue (binary mangled, size bigger than original).
The problem is caused due to Azure Logic Apps somehow mangling the “binary” contents in the body. The solution is to specify the body in another format, again, specified weirdly in the “official documentation” although there is no indication as to why this should work for “requests-to-logic-app” as compared to “requests-within-logic-app” (if that makes any sense).
You shall need to make the following change in the “Logic app code view” for this to work.
{
"Create_blob_(V2)": {
"inputs": {
"body": {
"$content": "@{item()?['contentBytes']}",
"$content-type": "@{item()['contentType']}"
},
"headers": {
"Content-Type": "@{item()['contentType']}"
}
}
}
}
do note: as of date of writing this, making any further changes with the “Logic app designer” will mangle the above to a static string, breaking it.
p.s. if you are indeed planning to use
@item()?['name']
, consider that email attachments do not
need to have unique file names :)
p.p.s. of course, we aren’t using the blob prefix as just
/attachments
, we’re using
/attachments/${yyyy-MM-dd}/${rfc2822 message id}