How to use Azure Blob Storage with .Net 6 | Azure Blob Storage Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back everybody today we're going to be covering Azure blob storage I'm going to be showing you guys how to upload download files or images as well as deleting and also getting what you have in a certain container and I'm going to walk you guys through the steps of how to actually create uh your blob storage container you know in Azure so let's start with what is azure blob storage so what is it it's basically Microsoft's Cloud solution for object storage as well as storing massive amounts of unorganized data you can use it to serve and store images documents files videos or audios any of these big things that you wouldn't want to put in a traditional database as well as you can you know have data backups or do logging or things of that nature but also Azure provides you know security and gives you a ton of space to store things where you don't have to worry about it so the next things I'm going to show you guys actually how to create these blob containers and then we're actually going to interact with them with the.net6 API that I have so let's move on to that but really quick I would really appreciate if you guys drop a like on this video so you can spread the more developers on YouTube as well as subscribe so you guys don't miss out on any of the other lovely content on this channel so I appreciate it but now let's move on alright so we are now in Azure first things first you're gonna need a resource Group so just create a resource Group whatever is closest to you under whatever subscription you may have uh just create one doesn't really matter what it is all you just need to do is you need to have one next thing is you need to create a storage account and that's what we're going to do together so once you're here at the storage accounts page what you're going to want to do is you want to create one make sure that the resource Group that you created is selected here and then create a storage account name it all has to be lowercase and once you type the whole and once you put a storage account name you can now just go ahead review and then go ahead and create once it finishes its validation create and then it should be done once you create your storage account you should be at a page that looks something like this we are now going to create the container the container you click right here go to your containers and now we're going to create one so the plus sign container so we're just going to call this blob container YT for YouTube and you can just leave it as private for this example and create once you have your container actually created I want to show you guys one thing first if you click on your container you can actually go in here and upload it manually uh whatever you want to upload straight into here so that is an option we're going to be doing everything from the code but if you needed to upload something manually into Azure uh you could upload into your container using this so just wanted to let you know that that is a thing you can do but the next thing we're going to need is we're going to need the connection strings to our storage account so going back there then you scroll down here to access keys and you're gonna see uh the name of your storage account and then you're gonna have your connection string so what you're going to want to do is you want to copy this one and then it's going to go into the API as a connection string like you would have in any other database so now now that we have this we are going to switch over to the API project and actually start you know accessing uh what we just created okay guys so we are now at the blob storage API API project so basically I just created a net 6 API and I already went ahead and added a few things and I'm going to walk you guys through what I already have in this project and what you guys are going to need uh but first things first since we just were in the Azure portal and we were talking about the connection strings please go in uh and add in kind of this Azure blob connection string or just your connection string and then paste in here uh that connection string you created for your storage account that's the one that we need put in here and now I'm going to show you guys the rest of this application so we can go through and then actually you know interact with The Blob storage I'm going to run the project very quick just to show you guys the four endpoints that we're going to have in our controller slash repository we're gonna have a get Blob file this is going to get an individual image or text file we are going to upload a blob file so we're going to upload a text file and an image and then we're going to delete as well as get a list of everything that we have in our blob storage container uh the other thing that I want to walk you guys through is that yes we have this controller we will be using Postman as kind of our front end but obviously you guys can think of it as you're gonna have some website some front-end site that would be interacting with this API and pulling these images or files as needed so all the files that we have here is we have this blob storage container very self-explanatory just uh you know calling out to the repository here uh this is the get Blob it's going to take this path and then you know send it off we have our post this is going to be basically the uploading a blob file which we're going to have this content model which I will show you guys in a second uh we get that from the body and then we send off the file path and the file name to the repository the delete is again just going to take a path and then listing our blobs takes nothing because we're just going to list everything it's just to get all essentially um so here we're just gonna have the interface which basically again is just going to define the Four endpoints that I already showed you guys then we have two models we have this blob content model this essentially is what I was showing you guys right here um basically it's just what we need to get from the front end or the client side uh to be able to successfully you know upload a file and basically it's just we need the file path and the file name file path to wherever you are uploading you know this image or blob item from and then the name to just make it a little bit easier and the second object that we have is this blob object this is basically what we are returning when we download so basically whenever we get this blob file this is what we're returning right here or we're turning right here and then returning this as a file so we have this blob object which we are returning a stream a stream of bytes or content and then we need the content type why because whenever you serve this back to the client side uh whatever it may be it's needed to know the content type so I can serve it correctly if it's an image and a PNG it needs to know that when it's going to the client side or if it's a text file or something else and I'll show you guys how I'm handling that in this API once I actually show you guys the code for the repository and the next thing I want to show you guys is just that the blob repository it's right now not implemented we will be filling this out very soon and then I already showed you guys the app settings that's the only changing there and then almost finally you go into your program.cs and you need to add this Singleton of a blob service client and then give it that connection string uh that we copied from Azure portal and then you already added to the app settings then you're just gonna put that in here and there's one other thing because we do need one Library uh for actually accessing uh the Azure blob storage I'm going to show you guys what library you actually need to install to make sure that you actually have access to Azure blob storage you need to install Azure storage.blobs this is very needed and we're going to be using it in the repository when we're actually doing these things so definitely install this and once you have that you're good to go and now we can actually go implement the repository we are now back at the blob repository and quickly I just want to say that this whole project will be put on my GitHub the link to that should be in the description if you guys want want to go get the code play with it check it out it will all be there for you guys first things first we need to add in some variables so let's go ahead and add those we have our blob service client and then our blob container client next thing we need our Constructor so we have our blob repository Constructor we're feeding in the service client and we're initializing it as well as we are initializing the client uh by basically going and getting the container that we created and right here we just put the name of the one that we created which is blob container YT in my instance so that is the string that I put there and we've now initialized it so now the one I want to do first is the uploading one we want to upload a blob file so that we have something there so that then we can do all the other ones so let's Implement that one first it's not that difficult but we need to do some stuff so like I said we have the blob content model which is what's going to be passed in if you guys look at here we are uploading a blob file and in our container for that one we have this from body this blob container model and then we basically just go get the file path file name and pass that into the repository so to actually Implement upload blob file we first need to create a blob client that blob client we are going to pass in the file name of the file that we are trying to create and then the next thing we're going to say is boom let's upload this upload async off of that we're going to need to make this method async as well to make sure everything's asynchronous and good and then we're going to pass in the file path to this upload async uh basically you know wherever your file is coming from wherever it's saved wherever it's stored it's there's always going to be a file path passed in and that's how it's going to know where to grab it from and then we're gonna send it uh because since this client is pointing to this blob container that's where it's going to be uploaded to and then all we're going to return uh is this absolute URI this URI is going to be a long URL uh basically with your Azure blob storage it's going to be in there I'm going to show you guys and I'll explain why I'm passing this back uh once I actually test this so this is basically all the code that goes into uploading you know a file to blob storage so now let's actually test it so I have a test file here called little file it's just a text file that says I got a little file on my desktop gonna upload it to blob storage that's all it says and then I have the thumbnail uh for this video right here so I'm going to be uploading both of these so this is a text file and this is going to be an image uh so let's go ahead and upload those and let's test it so I'm now running the API I'm going to drop this I have a breakpoint at the repository I have a breakpoint at the controller and we now go to postman and you can see I created my object uh raw Json and I have the endpoint right here so now let's test it should go through as we see we are getting our URL to where it is on my desktop and then the image is right here or the file name I mean and now we step through we're going to see that we create this blob client correctly everything's good we have our storage account here the actual container name and then we step through we're going to see that we're going to get a status so status 201 it's created so good it should be up in Azure we're about to confirm in a second and I want to explain that we are getting as you see this long URL of this basically points where an Azure blob storage your what you just created is and why do I say return this and why not just return okay well because let's say you are in an actual real world application and let's say you have an entry for some new part that your shop just received well you're going to take a few pictures of this and you have information on the given part well that information will be filed away in your regular relational database but you can't store the images of the part in the database right this is why we're using blob storage so this URL is what you actually want to store along with the entry of the part info as some type of list why because this points you to when you go and retrieve all the information and images for that given part in this example that points you exactly where in Blob storage it is it tells you your storage account it tells you the container and it tells you the name so then you can go and retrieve it all when the client the client side needs to actually you know show everything off so this is why I want you guys to store this and then I'll show you guys how to break it all apart so you can use all the pieces that you need when you do need to download this stuff back but moving forward I just wanted to explain that uh you should be good and I'm returning this URL like I said and I'm going to be copying that because we might need that in a bit uh and now just to prove that it was created let's go back to our container let us refresh and now that we refreshed we have our thumbnail.png it is right here it is created it is in Blob storage awesome now let's do that again for the text file to prove to you guys it doesn't matter what type of file you're doing blob storage doesn't care it's going to save it so now let's go ahead and do the text file so again I've now changed this to be the little file.txt which is this file right here I'm going to send it you see that we get everything the same thing but now it's the littlefile.txt go through and we should again now we get the URL for this file as well so I'm going to save that as well because we're going to need that for the next examples but again let's now go back to Azure portal now that I refresh you can see that the txt the text file and the image you're sitting right next to each other we have to change any code and they are just perfectly sitting in Azure blob storage but now let's how about we write the code to like list them out I want to see what we have in this container because I don't know because I'm ignorant because I don't remember what I just did five seconds ago so let's actually write the code to get everything that this container has in it and let's list it out to the client side so let's go write that code in the repository okay so now that we're back at the API the next thing that we're going to want to do is do this list blobs because I want to be able to show to the client side what we have so first I'm going to grab the string list uh and I'm going to create it here and then we're basically just going to do something very simple you guys might be surprised with how easy and painless it is so we take that same client uh blob container client object that we created up here that already points to the container and then we're just going to do get blobs async and then we're just going to Loop through all the blob items add them to this list and then we return this list as you can see oh we need to add this async as well and then we're gonna go back to the to the controller and in the controller what are we doing nothing we don't receive anything we just get this list blobs uh we call the repository and then we return OK with the result and this is all the code we need to access everything that's in this given container add it to a list and then show it to the client side so let's run it and let's test it now with our API running again I have the URL right here in Postman we are going to send I have a break point in here so as we can see we're in the repository we have the client again storage account blob container YT we are going to get our first one so what is this blob item this blob item is our littlefile.txt and has a bunch of properties in blah blah we're just going to add the name to our string list and move along and then we should see our next item is going to be the thumbnail.png and now I'm just going to fast forward and we get the list of you know what we have in our container so if we ever had a question of what we have that's how you just list them up but now that we have this let's actually show you guys how to download that text file back or download that image because that's where it gets a little tricky because we do have to pay attention to the content type so the client side or Postman in our case knows how to show off what it's receiving uh and it's very tricky but I'm going to show you guys my solution to that so let's dive into the repository again okay so I'm back at the controller and now how to actually download or get all the blob files that we already uploaded to Azure blob storage so first things first we have you know our endpoint right here we're going to be receiving the URL that URL that I told you guys to save and the reasons why we're going to be receiving it here and then actually splitting it up and using the different parts of it in the repository so now that's all this is and we're going to be returning a file with its content type which this is very important and then a byte stream or whatever content it may be so now in the repository let's start coding we need to get our URL we need to then extract the file name so new URI get the segment and then get the very last one because we want our file name if you look at Postman our last section is the file name and then also the extension are two very important things that we are going to need and when I mention extensions we are going to be able to decipher what is an image and what is not because it makes a big difference when it comes to actually serving the correct content type so I'm quickly just going to make a small list here called image extensions and this is what we're going to use to detect is the content that we're downloading an image or is it something else and obviously as this goes along you guys could add or play around with whatever you know image extensions or whatever videos whatever you guys want to detect you could add into here uh and then you'll see how we actually use it in a second so I went ahead and just added a try catch and now that we added the try catch we're going to do something that we've done on some of the other ones which is basically just create a blob client using the file name that we extracted here and then I'm gonna do an if statement next using that given blob client I'm going to check hey does this file that we actually created The Blob client from does it actually exist if so we can continue actually trying to download it if not return null or you would have some other type of error case where you would return something letting know like hey this is not actually correct but moving on in the case that it does exist which in our case it's going to we are going to need a blob download result object called content and we're going to basically say on the blob client download content async so this is basically where we're actually going to get the content from Azure blob storage and now we have to manipulate it parse through it and make sure that we extract from it what we actually need the next thing I actually want to do so we can actually pass this data all the content correctly to the client side is that we need to actually turn it into a stream a stream of bytes so that's going to be the downloaded data here so basically the content of the download result the content actual object of it to stream and now the next thing is probably the most most important part to actually getting these files and this is the way I do it there's probably other variations but this is the one I'm using and I'm using the string list that's up here so as you see I added another if else in this if else what am I checking well I'm going into the image extensions which is this list of strings and I'm checking does the path extension of this file name so like uh littlefile.txt or thumbnail.png do either of those path extensions exist in this list because if it does exist that means that I'm returning an image and I want my content type to be set as image else I do not care about the content type and we can just use the default one and this is very critical so that your client site can actually correctly show the image or show whatever type of content that you're passing correctly so the next thing if it actually is the extension that's in here which in one of our cases it will be we are going to take that extension from the file name and then we are going to return a new blob model and that blob model was which model it was the blob object model which contained just what the content which was a stream if you guys remember a stream and then a string which was the content type so right here we're just going to pass back to the stream of bytes that doesn't change but then our content type here we're basically going to remove that period uh and we're going to build image basically backslash PNG in our thumbnails case and then in the case of our text file we're gonna have something slightly different we are going to just have the regular content type that comes with it and that's how I'm basically checking are we getting an image or whatever else we could possibly check for or are we just returning the default so now let's go ahead and actually try and retrieve both of the items from Azure blob storage and see if Postman knows what to correctly do with them I have my API running again so now let's actually go and get that thumbnail.png that we uploaded to Azure blob storage so I have my endpoint right here I wrote the whole thing already blob storage backslash get Blob file and I have my URL uh query parameter and then I'm going to copy and paste in that path that I told you guys is what we would in theory in a real application store and we're going to have that there append that to here and we're gonna send and now I have a few break points so we see that the URL we're getting correctly in here and we've hit this so now it's moved through now we are in the repository let me zoom out a little bit first things first this is going to get the file name so we've already extracted from this URL the storage account blah blah blah blah blob container YT thumbnail we already got the file name we're gonna get into the first part create our blob client now we're going to check does it exist and unless something's changed from this it hasn't it it hasn't it's there so we should step through we now get our content and I'm going to turn our content to streams so we just see that we have a memory stream here of a certain length so there's actually content there and now we're going to check does thumbnail.png exist here well I hope it does so we should step through and it should hit into the first if now we're going to get that extension of dot p and g but now I need the content type to be image backslash PNG so that it knows on the client side that we are returning an image and then we're just passing along our downloaded data so I'm going to show you guys the object when we step through now our content here is result of blob object and we have our memory stream and then we have content type of image backslash PNG and then we're just returning a file of that and we step through and Postman is able to successfully show the image of the thumbnail because we did the correct content type which is why it is so key but now let's go and get littlefile.txt so without doing anything different I'm just going to go in here and change this path and now we send again we should see that now we're getting little file.txt we're going to step all the way through none of this is going to change it should exist but now when we get to this if statement it's not going to have dot txt in here so what we're going to do is we're just going to return the content type of octet stream which is like the default and then our content which is there as well and it is obviously a different length from the image because it's a much smaller text file and now we step through and as we can see it's printing out the text from the text file if you don't believe me we can just show them both side by side right here same text so it was able to grab it and display it successfully but now let's go and do the final one which is just let's delete one of these items so I'm sick and tired of thumbnail.png and little file.txt I want to delete them from my Azure blob storage so it's a very simple amount of code to actually remove it uh from blob storage so let's actually move on to that when it comes to our controller there's not much to it it's just HTTP delete and we're just getting that same path oh that same long path and we're just gonna do and call delete blob and then just return okay so in our repository what are we actually going to do so in our delete blob first I'm just going to make this async really quick and then the first thing that we need to do is out of that path we're going to do exactly the same thing that we did right here and that's basically we just want to extract the file name so we can just specifically tell Azure blob storage create this blob client and then if it exists delete it so we create that blob client and then the next thing is the simple thing of just saying hey blob client just delete it if it exists we already gave you the file name so look through check is it there if so get it out of here and that's all the code that you need to actually delete something from this given container in Blob storage so now let's actually get rid of one of them so I have my API running again and now we're at our delete so again we have the same URL delete blob and then we again also have a similar path and then the long path so we are going to use littlefile.txt because I'm just so sick and tired of it so now let's actually send I have a breakpoint there and let me add a breakpoint here just to make sure that everything's good now let's send so as we can see we're hitting it we're getting the path correctly to little file we're going to step through grab the file name create the blob client and then if it exists which it should delete it and then we should see nothing except 200 okay that means it worked and now let's actually go and prove it over here by refreshing and now that I refresh you can see that little file.txt is gone from Azure blob storage but that's it guys this is all the code that you actually need to You Know download an image or a file upload an image or a file delete anything from blob storage as well as just listing out everything that you may have in your container as well as I showed you guys how to actually create everything that you need your storage account and your containers and you now have this wonderful.net6 API with these controllers and these repositories and you may be thinking now well how do I actually unit test this stuff well I got you guys so watch this video for all the information on unit testing.net6 API
Info
Channel: Israel Quiroz
Views: 4,225
Rating: undefined out of 5
Keywords: uploading files to azure blob storage c#, uploading to azure blob storage c#, uploading to azure blob storage .net 6, blob storage .net 6, AZURE BLOB STORAGE In .Net 6, azure blob storage .net 6, Azure Blob Storage Tutorial, blob storage, azure, azure blob storage, azure blob storage dotnet 6, download image from azure blob storage c#, download file from azure blob storage, upload image to azure blob storage c#, azure blob storage demo, azure blob storage with .net 6
Id: dRx1i1-ditE
Channel Id: undefined
Length: 24min 56sec (1496 seconds)
Published: Mon Oct 10 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.