lokanath das
lokanath das

Reputation: 916

How to send File from Logic App to function App?

I have a Logic App which Reads the Mail content along with its attachment. I need to send the File /File content (file is prefered) to a function App.

Later function App interacts with a 3rd party API, need to pass the same file to that API.

NB--> I am able to fetch the File bytes in the Logic App. But while passing the Bytes to the function App through Json structure, Json Deserialization shows issue @function App end.

Upvotes: 2

Views: 4177

Answers (2)

Ravi Anand
Ravi Anand

Reputation: 5544

you can send file from logic app to azure function as attachment content.enter image description here

On Azure function side you can read it directly as follows:

#r "Newtonsoft.Json"

using System;
using System.Net;
using Newtonsoft.Json;
using System.IO;
using System.Globalization;

public static async Task<object> Run(HttpRequestMessage req, TraceWriter log)
    {
        try
        {
            log.Info($" my function was triggered!");

            string jsonContent = await req.Content.ReadAsStringAsync();
            dynamic data = JsonConvert.DeserializeObject(jsonContent);

            if (data.fileName == null && string.IsNullOrWhiteSpace(data.fileName))
            {
                return req.CreateResponse(HttpStatusCode.BadRequest, new
                {
                    error = "Invalid json parameters!"
                });
            }
            string fileName = data.fileName;
            var fileData = data.fileContent;
            string val = fileData.ToObject<string>();

            var base64EncodedBytes = System.Convert.FromBase64String(val);
            var result = System.Text.Encoding.UTF8.GetString(base64EncodedBytes);
            // log.Info($"file data val : "+result);
            StringReader stringReader = new StringReader(result);
            //rest you can do yourself
        }
        catch (Exception ex)
        {

        }
    }

Hope this helps.

Upvotes: 3

Vladislav
Vladislav

Reputation: 2992

I would use Azure Blob Storage as an interface between the Logic App and the Function App.

At the Logic App an Azure Blob Storage standard connector could be used, as per this documentation. Create Blob action could be used which expects the file bytes that you can retrieve.

The Function App could be bound as a trigger to a specific Azure Blob Storage container. Here is an example:

[FunctionName("BlobTrigger")]        
public static void Run([BlobTrigger("blobcontainer/{name}", Connection = "StorageConnectionAppSetting")] Stream myBlob, string name, TraceWriter log)
{
    log.Info($"C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes");
}

Upvotes: 2

Related Questions