Asp.Net Core API File Upload to Azure Blob Storage

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
I'm not sure why but people tend to get scared when they need to upload files via an asp.net core API tool for instance either blob storage are you in the same situation does the idea of managing files through an asp.net core web API give you goosebumps in this case this video is right for you because in this one we will cover everything you need to know to get very familiar with uploading files through asp.net core to Azure blunt storage [Music] all right hey there and welcome to the cool drinkers Channel uploading files through an asp.net core web API to Azure blob storage is a Brisk don't you believe me well stay tuned because in this video we'll Implement a files handling API from scratch with everything that we need to upload and manage files to Azure blob storage and there is also a huge bonus because we will not just upload files but we'll talk in depth about how we can manage files how we can download files when we would need to download files or we delete files and everything around that here we have a very basic API that we will build out throughout this video and we have some very basic methods dedicated to file management and we have first of all list all blobs in which we will list all the available blobs in a certain container that might be useful because consumers might want to know exactly what files do we have in our container then obviously we have this upload and this download methods that would upload files and also download the files and we will talk a little bit more more in depth about why we would need such a download method in our API and last but not least the last thing that we would like to do probably with files is to delete them when they are not needed anymore we also have created a storage account and we have already created a file container which is empty by now but we'll see that throughout this video we'll have to play with a lot of files and we'll see that we can add files to this container through our API to get started I would like first to create some models that we would need while working with this API and the first model that we will need is the blob video and here in this blob video we will have a few properties like the urea I often blob the name of the blob the content type and the stream like the content of The Blob if it is needed and then I would like to also have another video that would be dedicated for actions that implies management of the files like uploading or downloading or deleting this might be kind of like a blob response detail so let's add this new class here and let's call this globe response dto and this blob response detail would contain this type of properties like the status like if we had an error and the blob itself that we have managed and this once again this detail is or we will be using it for actions that would imply in one way or the other kind of like uploading or deleting the blobs next we'll need to create a file service and let's click here add the new class and we'll call this file service okay and in this file service we need to have all the information that we need to work with the error blob storage so I will add here some information like the storage account which is this storage account the key then we have this blob container client but in order to be able to use this we need to add a nuget package let's go to manage nugget packages and that new nuget package should be Azure storage blobs so let's install this okay done and now we can just implement or add the missing using and then we will leave the Constructor obviously and in the The Constructor we will kind of like initialize all the clients that we need that would be something like this but once again import the missing references and now we should be good to go we create these storage account shared key credential based on the account name and the key so that we are able to authenticate and use our Azure blob storage in our story Azure storage account and then what we do egg actually is reuse this globe service clients to create already a blob a container client that contains already a reference to our container where we will manage the files like the files container if you are not 100 familiar with Azure blob storage and how to work with Azure blob storage from a.net application I have already created a video and you can see a link to this video on the screen right now in the corner upright and obviously also in the description of this video also if you like this type of content don't be don't forget to hit the thumbs up button and like that video it will make it easier to discover for others and if you are for the first time on the called wrinkles Channel hit the Subscribe button and the notification Bell so that you are always notified whenever we have something new on this channel now that we have this behind us let's go to implement the methods that we will need in the service the first one will be the method to list all the blobs async and what we do here is weekly this list of blob dto Globe detail is our detail that we have created previously and then what similar to what we have done in the previous video we can reflect a wait for each because this that's an eye async enumerability is returned and we get the blobs async and then what we do is we create the URI and we build a full URI and then based on that full URI we are able to create a new instance of The Blob dto for each one that we iterate through and then at the end we add this basically to our list and then we return the files so it's very easy nothing very complicated and very similar to what we have done previously like in the previous video that I have mentioned earlier and now comes the more tricky part how do we actually upload files when we have an API and I have already created a method here and I want to explain this and when we are working with apis we usually get the files in this form of an iPhone file this means theoretically that files should be posted actually as formed data and we look into this when we will run the API and exactly we'll see exactly how the file is actually uploaded now the idea and why this is this is a little bit more tricky is because in this case when we are using with an API and we have this iPhone file we don't have the file somewhere on the disk so we can we can just simply provide a file name and a pad to the file and just upload it to Azure blob storage in this case what we would need to do is we need to use a stream to actually upload the file to Azure blob storage and that's exactly what we do here first of all we create a blog client based on the file name that we receive in this iPhone file and then what we do is we create a new stream that contains the data and we open the stream stream and then we use the client to upload async but we provide the stream instead of providing just a file path now what we do is obviously when everything is ready here we create the response status error in this case is false once again here I want just to show you exactly how we can work with Azure blob storage in an API and upload files obviously here we would have try catches and catch exceptions that might be thrown anything similar to that and this response might even be error in certain cases if we get an error back but now I just wanted to focus on the functionality and then obviously what we do is we return the response when it is ready and the next method that we want to implement or action is downloading file so it's basically the reverse of what we have done previously now when downloading file here the things are a little bit different because in this case the consumer just need to provide us a file name and we need to kind of like check in the container if we have a file with this specific file name so if the blob exists and if The Blob exists we kind of like need to download it as a stream and return it to the consumer also as a file so that's exactly what we do here once again as we did in all the other methods we just we just create a blob client having this blog file name as an incoming string parameter and then what we're doing is exactly this we hear a weight file exists async and this will return us an answer or it will return true or false like if the files exist then what we want to do is okay we want to once again uh open read async so we open the file and then we have the stream blob content equals data so we get the data into a stream and then a Content a wide await file download content async so this downloads the file itself and when this method executes and is finished we have basically a file and then what we do here is we capture also the file name and the content type and based on this we create this new blob dto that we want to return which contains basically all the data and the file and everything or all the information that we would need to kind of like provide to the consumers last but not least we need to implement a delete method and the delete method is probably the easiest one to implement here in this case once again we just need to get a file name as a string and what we need to do is kind of like check if this blob exists with that specific file name and if exist we have this await file delete async and after we delete the file we once again return this blob response detail now that we have the service The Next Step would be to add this service to the dependency injection container an important thing to note here is that in the official Microsoft documentation it stands written then you can register this service or services that work with that Azure storage blocked SDK as a Singleton service so let's do that therefore let's add our file Service as a singles and service to our di container and I will also reference what is my missing here and we should be good to go and we can move over to our files controller and we can take right now these methods all of them one by one and Implement them before we do that obviously we need to kind of like use the private fields to have or to get a file service instance in the Constructor through dependency injection let's make a little bit of space here and just add everything that we need so the private read-only field for the file service and then we get the file service in jacket and now we can Implement all the methods that we need here for the least async the things will be very very simple what we need to do is kind of like just use the file service and perform all the actions that we need and this is very straightforward we just get the list of blob dto and then we return this OK and the result then we have this upload file that we need to implement and remember we have on the service that upload async method that we have created and we can use this providing this iPhone file or method and it will upload this file to our favorite blob storage instance and now comes once again the more fun part which is the download and the implementation for the download it would look something like this once again we have this file service and we say Here download async and we provide this string file name however the important thing to note here is that in this case we don't return just a regular OK result in this case we want to return a file because that's that's what the download is all about we don't return Json in this case but we return a file we have returned file with the result the content the content type and the result name I'm sure at this point some of you might ask but hey Dan why do we need to implement also download functionality because when we upload files to Azure blob storage we get a URI back and based on the URI we can actually use it for instance in our single page applications if we want to display images my answer to this question is yes if if it comes to handling images and using images in your single page application that or having or working with the URL from Azure blob storage might just work but file management in systems that we build usually go Way Beyond just having to work with images a lot of times you need to be able to download files like maybe your application generates some reports and those reports need to be downloaded by user or maybe other types of files and last but not least we would also need to implement this delete method and that's very simple to implement we just use the file service once again and provide the file name that we get here as a param in our action and then we just execute the delete and we return the result also with an OK in this case so now we have implemented everything so the obvious next step is to see how everything works so let's debug actually the application because I also want to place a breakpoint here in this upload so let's debug the application okay so first of all we know that our container is empty so the first thing that we need to do is to add a new file and if if we go here to Swagger we see that if we want to try it out we need to kind of like have this file uploader and what we can do here is just use this to upload a file that I already have we created is this code wrinkles.txt so let me just open this file now and right now you see that when we will send this request it is not sent as application Json but it is sent as multi-part form data what this means is that if you have a react application or an angular application or Pilot matter even a regular JavaScript application if you make a request to upload a file you do need to make that request as Json but you need to make that request as multi-part form data and in that case everything will work like a charm so let's click on execute and we hit our breakpoint and if we take a look into what we have here in this file we see that we have kind of like some information like the content description like the content type the file name and we have also the data of the file we also know how long the file is so we have a bunch of information about the file that that is being uploaded let me then just take away this breakpoint and execute the application further and theoretically now this should be uploaded correctly here you see that we have the response and the response even we get the URI that's also a very important part so if you are building this token API it's very important that when you return the response to the consumer it should also contain the URI or the specific blog now let's take also a look at our Azure portal and if we just refresh this we'll see that we have this brand new file here so everything was uploaded correctly let's go back to our Swagger in this case and let's for instance right now try the first method like I want to list them so try it out execute and that would obviously list me all the files they have or all the blobs that I have in this case I just have one with the URI with the name and with the content type now the next thing that I would like to do probably is as a consumer I know that I have this file with this specific file name so I might want to kind of like maybe download that file so the next thing that we want to test out is actually that download feature and this is this get method with this file name and if we want to try it out we need to give the file name and the file name should be called wrinkles.txt so let's execute this and we see that in this case we got the response at edit the response was actually a file we have this link of download file and if we click on it obviously it will be downloaded like it happens regularly obviously last but not least there is this delete method and in this delete method if we try it out once again we need to provide a file name and then execute and we see that it was successfully deleted so if you go back to the Azure portal and if we click on refresh we see that the file is gone so it's not here anymore that's it see it was not complicated at all and now I guess you can go with confidence and head over to your real project and Implement and start working with files especially when you want to upload them to Azure blob storage with a certain degree of confidence if you enjoyed this video don't forget to hit that thumbs up button and like that video so that others might Discover it easier also if you are for the first time on this channel hit the Subscribe button and the notification Bell so that you are always notified when there is some new content on the channel and if you have any type of question or just want to get in touch with me don't be shy and head over to the comment section of this video and leave a comment and I will be more than happy to get in touch with you this being said thank you very much for watching and until the next time I wish you the very best
Info
Channel: Codewrinkles
Views: 6,472
Rating: undefined out of 5
Keywords:
Id: DzQ7CNnb9yM
Channel Id: undefined
Length: 16min 9sec (969 seconds)
Published: Thu Mar 23 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.