How to Create an Azure Function to Zip Files via Power Automate or Logic Apps

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi there folks in today's demonstration I'm going to show you how you can build an aure function that can compress or zip files from base 64 content so this scenario is useful if you want to compress files from different sources whether that be SharePoint or one drive or third party apis blob storage Etc there isn't a native functionality in power automate to do this but not only that I'll show you how easy it is to build an aure function so that you can extend the functionality of power automate and keep it all within the Microsoft ecosystem using Azure so if that's something that interests you please make sure you like And subscribe and without further Ado let's jump into my demonstration so I'm here on the Azure portal and the first thing that I want to do is I want to create a resource for a function app so I can click on the create a resource button here or as you can see I have the function app because I've already created a function app in the past so I'm going to go ahead and click here and this will allow me to see all the function apps that I have on my 10 so at the moment I have none I'm going to create a new one and then it's a case of working through this wizard so you can see I already have a subscription it's my free subscription I have for all my development work I've got a resource Group which I need to choose in this case I'm going for mine called as your Automation and that's how we group together all the different resources and makes it easy to to filter them and I need to give that function app and name which will then give us the prefix or the URL for our rest API so this function name I'm going to give it the name of demo Birds zip function there we go and then I just need to work through the rest of the options so it needs to be in net because it's um some C code I'm implementing and everything else here consumption is how I want to be charged for this and everything else is pretty much default you can work your way through these settings and make changes if you see fit but I'm going to keep everything as default and we will review it and hopefully there'll be no errors and I can create so after about 15 20 seconds we should get a new function app being deployed and ready for us to implement some code so the deployment now complete I can click on go to Resource and that will now allow us to go and Implement some code for our new function app so from our newly created function you can see that we have the ability to create a function and I'll go ahead and click on that and we have another wizard where if we select the HTTP trigger we can then trigger using rest API from Powershell or power automate or logic apps and if I scroll a bit further down the screen we can see that our function has a name so let's call this zip files and we can change the authorization level but for this I'm going to leave it at function um and we'll hit create so with this new function created if I go to the code in test tab on the left hand side we already have some sample code and we can use the test and run Pane and you can see there's a sample body we're going to be posting to this new rest API and if I hit run it'll run this code and give me the response but we want to be able to of course zip some files so I have a blog post and I have the code that I've written using chat GPT to zip and files based on an array of file names and base 64 file encodings so if you jump across onto that Blog the URL will be in the description below you can copy that code and simply overwrite the code that we have in here the sample code and then when we go into test and run it'll run this new code and allow us to zip some files but before we do that I'm just going to hit the save button so also included on my blog is some sample body input because of course we don't need an object with a key called name we need an array with the file name and also the file content so if I overwrite that with a sample from my blog we can see we have a couple of files in here that contain a text file and also an image and you'll note that both the file contents are in base 64 so that's very important when you're passing the data into this function app from Power automate or from Powershell Etc that the string is encoded in base 64 so if I go and test that and run it what I should get is a 200 status and it looks like that's what we've got here at the top here a 200 okay and you can see the file content here is actually the response including the content of a zip file so if we were to save that into a file Now call itzip we'd be able to open it up and it would have a couple of files in it a text file and an image so that's pretty much all we need to do to set up our function app there is a get function URL at the top here we need to copy that for our power automate flow so I'll go ahead and do that now and then let's jump across and build out our flow so from within power autom at if we go ahead and add in an HTTP action we can then send a post of the data the body into our new HTTP function that we've created so I'll choose the HTTP action I can paste in that URL that I copied earlier change the method to post and the thing I need to include is the body so if we think about how we do this using a folder of files we' go and list all the files within our folder we can then go and get the file content of all those files in the folder and then we can create an array just like we saw in the blog post or as we did in our test on the aure function within the as your portal but if we're just looking to test this very quickly and create a zip file again using that sample data that's on my blog post if I paste that in it's exactly the same that we saw before it's got two files it's got the text file and it's got the image file and all we need to do is to save the output into a create file action so I'm going to go here here and search for create file I'm going to use the one drive action cuz I've got my one drive already open in another Tab and I will choose a folder which happens to be called a folder to zip and if I give the file a name so we'll call this a new zip file.zip don't forget the extension and then the file content is just be based on the body so I can select the body there and we now have a flow that will take an array of objects which is our file names and our file content and then with the output it will save that as a new zip file and we've created a zip all from Power automate using our new isure function once we've tested it I'll show you how we can expand that and go and create that array dynamically so I'll now test this flow the process runs extremely quickly we can see if we pop open the history for the HTTP action we've hit that new URI that's been created for our function we've passed in the body with the text and the image and we've finally got a create file that saves a new zip file to a folder to zip and if I jump across onto my one drive and then just refresh this screen we can see we now have a new zip file.zip which I can open it contains a text file it contains an image file and if we go ahead and download that and open it we can see now I have that new zip file and if I open up my text file it contains the text hello world and if I open up our image it's a picture of a peach don't ask me why but when I searched for b64 images I got a picture of a peach so there we go so let's now see if we can make this Dynamic and actually zip up all of the files that are contained within this folder called a folder to zip so we will zip up our newly created zip include the Excel document and the word document in a newly created zip file so back over on to power automate I'll put our flow into edit and the first thing I want to do is to add in an action to to list the files in the folder so we'll search for list files and folder there's the one drive action and of course I'm doing this for one drive you could be getting files from a secure FTP from an FTP from an API it doesn't really matter as long as the file content is in base 64 so I'll choose my folder location which is my folder to zip that will list all my files and give me an array of all those files then what I want to do is I want to get the file content so I'm going to use the fact that power automate will automatically put me into a loop if I choose get file content and then for the file ID if I use the dynamic value for ID you'll see that will'll get put into an apply to each because of course it could be one or many files it's returning an array or a table of those files in the folder so we're now looping through all those files we're getting the file content and then we want to create an object so I'm going to do that by adding an action and using a compose now you've got two options for creating an array one is actually to initialize a variable outside your apply to each the other is to use a compose and you can just go and specify your object structure so an opening and closing bracket like so and then we've got those two key value pairs the file name and the file content now like before I'm going to copy and paste the sample that I had on my blog post but I'm going to strip out the first object which is our second file so I can copy everything between the squiggly brackets and delete that I also want to make sure I delete the comma there so I've now got a single object in an array but I also don't want an array I want just an object so I can delete the square brackets so tidying things up a bit here we now have everything that I require between two squiggly bracket the alternative of course is just to type in file name in double quotes file content in double quotes put semicolons on after them and just ensure you've got a comma after your first key value pair so at the moment this is fixed the file name and the file content is fixed but we can highlight the text file. text we can go and choose some Dynamic content we can go and look for the name of the file so there we go the name of the file because we're an apply to each you'll appropriately get the name and then because we're using the get file content again we can go and search for dynamic content choose file content and we've now created an object in the structure that's required for our function app it has the key for the file name with the dynamic name and it has the key for the file content with the base 64 encoding or does it it just so happens that this file content is also an object so it will fail um the correct expression for one drive and there are differences between one drive and SharePoint is to include a key called content dollar content so I actually need to change this file content expression so if I click the cross and remove that Dynamic value I can go into the expression editor I can go into Dynamic content I can choose file content which gives us that expression that we saw but I need to also add in single quotes and the square brackets dollar content so if you have problems with this make sure that the value that you're sending into this object is base 64 and the best way to find that out is if it fails is to look back over the flow history and we can maybe have a quick look at that once we've run this flow so I'll go ahead and add that that is now the correct expression that will get the file content in base 64 so next up we need to make a change to this HTTP action of course at the moment we've got a fixed body because we tested it with that sample body from my blog we can delete that and we just replace it with an expression so if you had a variable you could put in your variable Dynamic content but because we're using a compose we can type in the expression outputs and we can put in single quotes the name of our compose action in this case it is compose if you called it compose object or compose something else then make sure you reflect that appropriately but what this will do is it will take all the instances of the compos within the apply to each and turn it into an array CU remember this body here is expecting an array of objects which is basic basically a table of rows with the file names and the file content so I'll go ahead and hit add and we can save and test so the flow complete successfully within a couple of seconds we've got all the file content listed all the files from the folder we've then got the file content here within our Loop which we can see there are three because it will have got that word document the Excel document and also that zip file that I created the compose we can see is just if we show the raw input is the file name and the file content and this is the important bit making sure that it looks like this it is base 64 encoded it's not some other funky string that you're passing if you're getting any failures remember I said we needed that dollar content if we look at the output of the get file content and we show the raw output we can see that we have our body and it's in particular this key that we want to send so that's why I changed the expression from body to body and then the question marks brackets and single quotes dollar content because we want to get this base 64 so we can pass that across into our your function so everything's run fine if I jump across onto my one drive and do another refresh oh I realize that uh I haven't changed the file name so I have actually overwritten our original zip file with a new zip file that should contain both the original zip file and the Excel document and the word document so we'll go ahead and download that have a look we can see we have the new zip file we have the Excel document we have the word document and if we do download that and just test it we can see the new zip file has those three files the original zip file within the zip file has still got the image of my peach and also the text file but if we go back to the root of that zip file we have the Excel document which contains some information about logic apps versus Power automate and if I load up the word document it contains the content of my blog post that I've shared with you and so there we have it we have built an auro function it's allowed us to zip files based on Bas 64 content we've tested and built it within the azour portal and then we brought it into a power automate flow that's therefore enabled us to zip up files and save it to our final location as always thanks very much for watching if you haven't already make sure you like And subscribe and I'll see you again sometime soon thanks for watching cheers
Info
Channel: DamoBird365
Views: 997
Rating: undefined out of 5
Keywords:
Id: u2c4-acjWuU
Channel Id: undefined
Length: 15min 17sec (917 seconds)
Published: Sat Jan 06 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.