Working With Azure Blob Storage in .NET apps. From Zero To Hero

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
I have never worked on a project where we didn't need to manage files the one way or the other and when it comes to managing and storing files Azure blob storage is a go-to definitely so in this video I will show you everything you need to know to get from Zero to Hero with Azure blob storage and your.net application [Music] hey there and welcome to the code wrinkles channel the Microsoft Azure storage platform is a very versatile solution when it comes to storing data objects in the cloud and I have used the word data objects because actually we can use different types of data to store in the Azure storage and therefore the Azure storage platform provides us different services to work with different types of data and the main services are the blob storage which obviously is used for storing blobs or files usually then we have the table storage that is used to store tabular data then we have the Azure Q storage that helps us when we need very lightweight Q messaging communication and last but not least we also have Azure file shares which as the name implies gives you the opportunity to create your file shares directly in Azure I could create an entire course on Azure storage capabilities but in this one I would like to keep everything very concise and we will concentrate only on ever blob storage so Azure blob storage is optimized to store massive amount of unstructured data as developers most of the time we use this for storing files to get started with almost all storage services in Microsoft Azure we have a central control plane that we call a storage account so let's create one I have already created the resource Group for this video so I will go on in this Resource Group and we will create a storage account here let's search for storage and let's choose the storage account and then create and here we will have to walk through some wizard some steps where we will have to make some choices that might affect the way that our storage account works and we will discuss this in a lot more detail the first thing that we need to provide is a storage account name then the region I will just leave it leave it as it is for now just to not spend a lot of time on that then performance here is also a basic decision or choice between standard and premium obviously premium is a little bit more expensive than the standard one and for a lot of scenarios even production scenarios standard is just okay now the other point that we have to choose here is redundancy and here we see that we have this drop down and we have these four options like locally done Storage durating storage Zone redundant storage and Geo Zone redundant storage now I wouldn't like to really get into detail into what each of these options mean because I think they also have a very explicit description here and I will go with this one I would just want to say that when you are working with either blob storage and the storage account for your production applications just make sure to understand what exactly this data storage requirements are from your customer or from your company and choose here a redundancy a strategy that is in accordance with the requirements that you have so I will just leave this one for now so let's go to the next one and here we have some checkboxes some of them are already checked some are unchecked and here I will leave everything like it is for now basically what we have here are some options regarding if you want to have only https and that's surely something that we want we want to have to be able to kind of like use access Keys we want to also enable Public Access that's actually a very important point because in some scenarios you might want in web applications to use URLs from your blobs directly in image URLs and in that case of course that this checkbox also needs to be checked now I believe everything else exactly like it is right now and I will go one step further and here we have this some settings for the blob storage and some of them are unavailable to change because we haven't chosen that premium version of that and then allow cross-tenant migrations that's for instance if you want to transfer data from one storage account that belongs to one tenant to one organization to a different organization I will leave this checkbox like it is right now and here the access tire that's also a very nice thing because you can choose here hot this is for frequently used data so data day to access very frequently or cool which like for data did you access a little bit more infrequently and that's actually also another way to or this is also another way to kind of like optimize the cost of your Azure Services now let's click next and go to the networking here with this enable network access here I would also like to choose enable Public Access from all networks then for the routing let's use the Microsoft routing we don't want to get into very complicated stuff here now here there are some other options regarding data recovery I will also leave here all the defaults that are checked and also those that are unchecked and let's click next on encryption by default we have an encryption address here at Microsoft Microsoft does encrypt our data but if we want to bring our custom key for encryption we can definitely do that next text that's something that I will Skip and then there is this General review and once everything is reviewed and all the details are there what we can do is just click here on this create and this storage account will be created well now that everything is done let's click on go to Resource and here we are at our storage account and one very important thing that you'll see here is you see that here we have containers file shares skills and tables so it's exactly what we have discussed earlier that Azure storage actually offers us a lot of different capabilities or Services however before we go further I would just like to kind of like look into two very important things and note them down because they will be important just a little bit later first of all just make sure that you copy this storage account name and then use and that you store it somewhere safe because we will need this in our.net application and then the other thing that we will need is to be able to connect from our.net applications to our Azure storage account and for this we will use some access keys so I will go here on the access keys and here on the key one we'll click on show and copy so we copy the access key then hide again make sure you save it somewhere because once again we will need it in our.net application now that we have an Azure storage account the next logical thing is to connect to our storage account programmatically from our.net C sharp application to achieve that we have actually three options and I want to very briefly describe them because they are very very important so first off we can connect or we can use Azure active directory to get access to our storage account now this is a very nice thing to do because in order to be able to do this you would need to create a service principle in Azure active directory and then you would use the service principle to be able to get X access to your storage account so it's very similar to when you register applications in Azure active directory for their API and in fact this would also mean that you would need to register your console or your application that wants to use the storage account as an application the second option that we would have is to use what it is called assured access Keys now shared access keys are very nice because they allow us to give a very granular permissions and access to only specific locations or specific parts of the storage account to specific people and for a specific time frame the last option is to use what we generally call Keys now the easiest way to connect to our Azure storage account is obviously using the regular keys however I want to note that this is probably the most insecure approach so when you want to do this inner production application you need to make sure that your keys are really secure they shouldn't be stored in app settings.json or in constants or anywhere actually where you or that you would commit to Source controls they need to be managed probably with some key Vault or something like that so you need to be very careful when you are using that however for simplicity's sake because in this video I really want to concentrate only on working with the Azure blob storage we will use this third approach with the regular keys to connect to our storage account but let me know if you want to have a more in-depth video on these different options that we might have to connect to different Azure services programmatically like Azure ad shared access keys and the regular keys and if I have 50 comments on this video that would request such a video I promise you I will create it to connect our dotnet application to our Azure storage account and to Azure blob storage and to work with the container that we have created first of all you would need to install a nuget package that I have already installed and that nougat package is azure storage blobs and if you have installed the package then you can go to the next step as we'll be doing a lot of stuff today I will create a new class dedicated to what we want to do here and I will call this class ever blob service okay and here we have this class to be able to connect as I said we will use the the regular access keys and for that we also need the storage account and I have told you that we want to kind of like note this information because we want to add this information as private fields in this class so first of all let's add the storage account name and then let's also add the key and then the last thing that we need is that blob service client obviously we need here to import a missing references and now that we have all this information let me create a Constructor and in the Constructor I would kind of like use all this information that we have here to instantiate a new instance of this blob service client what we'll end up having is something like this where we have these credentials and for this we have this storage shared key credential for which we need to specify the storage account and the access key that we have X actually from our ever portal and then we also need to provide a blob URI that contains also our storage account name so that's why we use this also here and with these two pieces of information we can create an instance of this blob service client now as we have the instance the next thing that we would like to do obviously is to understand how we can connect or if we are able to connect and for instance just list all the available containers to see that the connection works I will create here a new method that would be public it would be asynctask and we'll call this list blob containers async and I will just copy this because I will use this name later and here we have our containers and we have here blob service client and here with The Blob service client we can get the containers async and then we iterate through this IAC enumerable and for each container we just want to display the name so if we go back here to our program class let's move this console Redline at the end and the first thing that we want to do here is to to create a new instance of the service of our service equals new Azure blob previous so it's the one that we have worked on earlier and having this we would be good to go and then what we can do is just simple service list blob containers async and then we would be good to go obviously this is an async method so let's also await it and now what we can do obviously is run the application and prove that everything works correctly and voila here is the name of the only container that we have created so our connection to our storage account and to our containers is okay so we can move forward before we can get started with uploading file to Azure blob storage we need to get familiar with the overall architecture and this is important because the overall architecture also is visible in the Azure blob SDK that we will use and we need to understand that so first of all we have these storage account which is the central control plane that we already have then we have the concept of containers you can think about a container just like being a folder in which you want to upload or you want to play some files but then we have these regular blobs and when we say blob actually that we usually or most oftenly think about files now this is important because this exact scenario is also recreated in the SDK so what we have in the SDK is a blob service client and we can use this globe service clients to create a blob container client and this is a client that helps us to manage and to work with the containers now if we want to work with blobs we can use the blob container client to create an instance of a blob client and that block client will actually help us achieve what we want so uploading or managing blogs or files to Azure blob storage to spicing up let's start to upload files to our Azure blob container and I will use here this method and obviously I will explain it step by step because it's based on what we have this described earlier but the idea is that what we will return in the end is a list of the Uris of the blobs or the files that we upload to our Azure blob storage and this is obviously useful because consumers that kind of like want to upload files to to Azure blob probably they will also know if you want to know exactly the URI where those files will be uploaded so that's why we create this list of URI first and then we have here the file path for a file that I have already pre-created here is this hello txt and then what we do here is exactly what we described earlier so first of all we can use we have the blob service client that is something that we already have on our field and based on this blob service client we can use it to create a container basically not to create a container but create a container client for the files container that we have already listed and as we have a blob container client the next step is to create a blob client from that container client and here that's exactly what we do in this method here now there is a little bit of discussion here going on because Azure blob storage by default doesn't well use or doesn't have the concept of hierarchical data it only has the concept of flat data still we can kind of like use Virtual directories like directory that we create virtually based on the naming that we have or basic based on on the names or the structure of the file path that we give so the idea is that my file name is obviously hello.txt but I want this file to be in a virtual folder or virtual hierarchical folder that is called today so that's why I have created this today and here then this forward slash and then the file path and as we will see just in a few seconds this will result in the fact that on the error blob storage so if we go to the portal we'll be able to see this as a folder and then what we also want to do to say to see that kind of like this works and that they are two different locations means that we have to create an additional blob client because I want to upload the same file also with a subfolder or to a subfolder that is called tomorrow so in that case those two will be two different blobs so even if we have the same physical file on the file system they are two different blows and once we have all those two blob clients we use them one by one and we just upload the file to our blog now when we upload async there is this important part which see this through here and what we need to specify here is usually the file path where where we have the physical file that we want to upload and then if we want to override if that file already exists or not and the thing is that if we set this to True obviously it will it will overwrite an existing file that's exactly the same however if we have these two false and we have already that file uploaded or we have the exact same blob this will result in an exception being thrown so a little bit careful with this one and then we do exactly the same thing also for the second one also making sure that we have this overrated through because we will run this application several times during this video so I will want to overwrite it every time and then what we do is we simply return this blob Uris and obviously what we can do here we can go back in the program.cs and what we can do here is wait and say service dot upload async and that's it obviously we could assign this to a variable and then iterate through that list and then specify Orton and the list in the console all the URLs of those files but for now we will just skip that so let's run the application to and now that the application has completed let me go over to this um Azure blob storage and let me go back to my containers and if we go here to these files containers you will see that we have these two folders but once again these are let's kind of like logical folders virtual folders they don't really exist at the storage level and here in this today we see that we have this hello.txt and then also in this tomorrow we also have this error.txt dot hello.txt so you see that we have uploaded the same file but in Azure probe storage we have actually two different blobs like the file itself is the same but we have two different blobs like the file was uploaded twice let's say now we have a bunch of files in our Azure blob storage and one other very common scenario is that we need to list all the files or the file names at least to showcase the difference between hierarchical and flat listing of the blobs I will add here two different methods and obviously we'll go through them the first one is this get platin Blob async and here what we do here is well as previously we just get a broad container client and then for the blob we get all the blobs async and here the only thing is that you see that the return type of this get blobs async is an async pageable of blob item so it's also an async enumerable and that's why we also have this await for each now the idea is that then we iterate through all the blobs and for each blob we want to display obviously the name of the blob which is the file name and once again please note here the return type that we have from this method which is a blob item now if we want to display this hierarchically we do exactly the same things only the only thing is that we have here this get blocked by hierarchy async method on the blob container and if we take a look at what this returns this is actually a block hero hit item so the previous one was a blob item and this is a Blog hierarchy item now in the blob hierarchy item you have a little bit more information and context for instance if this is a prefix that will kind of like see if you are currently in a logical folder or virtual folder indeed in that case we just want to weld console rightline this specific folder and then we want to recursely recall the same method again to kind of like get all the information that we have in a certain hierarchy and if it is not a prefix then we obviously just want to write the Blog name now for this method to work properly obviously you need to have at least several different hierarchies and you need to have at least several different files or different blobs in each virtual or logical folder up other word or otherwise what we'll end up having is something pretty flat also but it will still help us enough to actually get an understanding on how that works so let me go back to this program file and first of all let's call service dot get flat blobslist async and wait service built you get hierarchy blobs list async also left at the console.writeland here it would kind of like be something similar to this one just to know exactly or to have a better overview for where exactly we have this one and when exactly we will have this one and let's run the application and we see the result here this time as said here we have this flat version and here we have this hierarchy version now the only thing is the due to the fact that we had only one single file in each other here we see it exactly the same way last but not least a very common scenario is two delete blobs or to delete files so let's do that deleting a blob is obviously something very very easy compared to what we have done before and it could be accomplished by using something like that so once again we have the filing bit we want and we need to create a blob container client and then based on the blob or from that blob container client we need to con to create a blob and then what we want to do is we want on the blob away delete if exists anything and what I want to show you here is that you have here different ways to delete you have the delete async and the delete if exists async obviously the delete async if it doesn't find the file it will true or it it if it will throw an error but then the delete if exists async then if it exists it will be it will delete the file if it doesn't exist it will simply just ignore everything and don't perform any action so to wire everything up here together we can then just go here to our program and a away dot uh services or service dot delete async and that would be good to go that's it now you have all the tools that you need to start working with Azure blob storage in your.net applications this being said thank you very much for watching and if you have enjoyed this video don't forget to hit the thumbs up button so that you help others Discover it more easier and if you are for the first time on this channel hit the Subscribe button and the notification Bell so that you are always not notified when we have new content on this channel and if you have a question or if you just want to get discussion going don't be shy and head over to the comment section of this video and leave your comment and I would be more than happy to get in touch with you the experience said thank you very much for watching and until the next time I wish you the very best
Info
Channel: Codewrinkles
Views: 5,533
Rating: undefined out of 5
Keywords: azure, azure storage, azure blob storage, azure blob storage .net, azure blob storage c#, .net azure blob storage, c# azure blob storage, azure blob storage .net tutorial, azure blob storage c# tutorial, azure blob storage .net course, azure blob storage c# course, azure blob storage for developers, azure blob storage for beginners, azure blob storage .net for beginners
Id: GhlMa3jx_XA
Channel Id: undefined
Length: 22min 6sec (1326 seconds)
Published: Mon Mar 20 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.