Getting started with Azure Blob Storage in .NET Core | Azure Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody I'm Nick and it's feet I'm gonna show you how you can manage other blog storage and your dog nel cuore application as a blob storage this is service for storing large amounts of unstructured object data such as text or binary data and you can use it to expose the data publicly to the world or privately in your application common usages include serving images or documents directly to the browser storing files for distributed access streaming video and audio storing data for backup or restore or even disaster recovery authoring data for further data analysis it's one of those services that are widely used in Azure and you should definitely know how to deal with it in your c-sharp code this video will touch uploading creating content listing and deleting in c-sharp this will expose my adversaries if you don't want to miss any episode please subscribe this amplification belt to get notified when I upload a new video now let's look at the code what I have here is I've made a small API here which has a single controller the blob Explorer controller and this controller doesn't actually do much all it does is it injects an AI blob service here and then we have a few endpoints one gets a blob by blob name and whenever I refer to blob remember it's just a file essentially so we can get a blob using the blob name we can list all the blobs we can upload a file as a blob or we can upload content which is a string essentially as a blob and then we can delete by name this is what this API can do all the basic you know crud functionality that you'd expect an API to do now if you actually seen my services I don't have an implementation of this interface anywhere so we will implement this interface from scratch and I'm not referring to blob storage anywhere yet so we're starting from scratch so the first thing you wanna do is you wanna go to the dependencies and you wanna manage and you get packages and you wanna look for the answer dot storage dot blobs package and this is the new v12 blob storage package so I'm gonna go ahead and add it and this will allow us to connect to our block storage now before we can connect to one we need to have one so what I have here is I create a resource group called blob storage eun and then I'm gonna go ahead and create an account the blob storage account so you wanna go to add and you can go to storage down here then it's this storage account blob file table and cue a storage account is a general thing but it supports many types of storage within it let's give it a name let's call it and see why the EU and and it all has to be lower case no hyphens no nothing it is in north Europe because my resource group is in north Europe I won't quite explain all the nitty gritties of blob storage currently cuz I'm planning to do a video just for blob storage without any code just explaining the service we're just gonna go with the standard performance we're gonna choose storage v2 don't go for this blob storage this is a legacy one you're gonna go with storage v2 and I'm gonna go with hot exists here hot means that my data is accessed regularly which is what this app probably represents if you don't we can you can go with cool but I recommend hot if you actively use that data and then I'm gonna go with local redundant storage here l RS which even that gives me eleven nines if I'm correct and then zone I think gives me 12 so it's very highly available you will not lose that data so let's go ahead and create that let's review the plan and I'm gonna push it and while I'm waiting for this to be complete and let me just explain that every account the storage account allows you to have something called a container and this is not to be confused with docker it's not where our application runs it's where our data is stored and you can see containers as logical separation of your data for example if I'm storing movies in one container I will store my images in another and so on and so forth so this is how the storage account view looks like and I'm gonna go with the container button here I'm gonna say new container and I'm gonna just name it YouTube and I'm not going to allow any public access I'm gonna go with private you can go with blob level read-only public access or contain the level read-only public access but we're gonna go with private cuz I don't want anybody to see that data I'm gonna click OK and this will make my container and now I'm gonna use this container to push data in it now we will programmatically upload but let me just show you how you can do it from the interface as well and you click the upload button and you see this pop up here and you click files and I'm going to select one of my thumbnails let's go with this one and I'm gonna say override if file already exists and upload and this very quickly uploads it in blob storage and then you can click on it and you can preview it and you can see that the file has been uploaded you can also download it and a few other things now before we go back to the code I'm gonna get a last thing that I need and this is the key to access the data so I'm gonna go to the account level let's click go to resource and I'm gonna go to access keys and I'm gonna use the connection string of course there's many ways to connect to other blob storage in this particular example I'm gonna use the connector string but you can use whatever you see fit and I'm gonna explain all the other versions in the azure blob storage video so back to my code I'm gonna go to the app settings of Jason and I'm gonna say how's your blob storage connection string and I'm just gonna paste that string here so with that out of the way we can go to the startup dot CS and do the registration that we need to do and there is one thing that we need to register and it's called blob service client so I'm gonna say services dot add single con and you wanna register this as a symptom you don't want to be mooing it up every time and we're gonna say new blob service client and what it needs in here is the connection string so I'm gonna use the configuration and I'm gonna get value string by name and the name is the one we gave in the settings so we have our client registration and this is what we will inject to allow us to essentially connect to block storage now the the next thing we need to register is the actual service that we will use which is essentially an abstraction over this client because it will be very hard to mock and use this in your unit tests so instead I made it an interface called I blob service and the implementation doesn't exist yet so we will create it now as you can see but this is how you register it and I'm gonna create a new class called blob service I'm gonna do AI blob service so implement that interface and add the missing members so these are all the unimplemented members and this blob service will have a private read only blob service client injected to it and we're gonna inject it through the constructor and now we will use that to implement these methods so let's start with how we can get a blob so we're gonna implement the get blob async now the first thing you need whenever you're accessing anything in the container is you need to get the container client which is a reference to the container that you're about to access so I'm gonna save our container client equals blob service client and then get blob container client and we need the name of the container that we are about to access if I go back to the browser you can see that we named the container let me just find it again we named it YouTube so we're gonna copy the name YouTube and I'm gonna paste it here and this will now allow me to access data in that container and in fact let's just start by accessing this part 1 dot PNG to test it so in order to implement that I'm gonna say what I need now is a blob client and in order to get that block client and it used a container client dot get blood client and what goes in here is the name of the file that I want to access the name of the blob that I want to access and to return the data I simply do return await blob client dot download async and I can't use a way that I need to do this hey sync and the reason why this says that it cannot return is because this is returning a blob download intro so let me just say VAR blob download info equals that not belong and then return new blob info and the two things I need is a stream of the content and the content type so blah blah run load info dot value dot content is the stream and then blob download info dot value dot content type is the content type and this should be enough to download the blob and serve it back through the API and in order to test that I'm gonna go ahead and run this API and I'm gonna use postman to get that image so the name was part 1 dot PNG and as you can see I have postman here and all I'm gonna do isn't gonna go to blobs for its last part one dot PNG and do send and this will preview the image for me because it knows it to me much in fact if I copy that and I go to the browser and I create a new tab and I paste that you can see that the image is loaded up so that's great it means it's working we can get data back from the blob storage account or the container so let me go ahead and stop it so this was simple enough now let's see how we can actually list all the blobs in the container and something I should mention is it's a bad idea to hard code strings like this because what if the container name change the whole class is obsolete now you would either inject that through the constructor or make the parameter in this method and every other method but for simplicity reasons and for this video I'm gonna just hard-coded of course you can find the full code in the description down below as always so in order to list all the blobs on it again a container client so I'm going to copy the same container in here and I will need to turn this into an async method again and now I want to create an items list which is a new list of type string just an empty list and then I'm gonna say I'll wait for each this isn't c-sharp ate feature which allows us to iterate asynchronously something that is essentially streamable and I'm gonna say blob item in container client dot get blobs async and then for each item items dot add blob item dot name and now I have a list of all the names in the container of all the blob names and now I'm simply gonna return that and that's how I implemented the second method which is the list blobs method and if I run this application let's see how it looks in postman so in order to get that I need to just create a new tab I need to do blobs first last list and as you can see part one is in fact the only item in this container and this array returns that so great that's how we now list items from the blob storage container let's go to the next method and this is a bit of a more tricky one because we actually need to upload a file from our drive what I have here is a bunch of thumbnails that I create for the for the series and we're gonna try to upload another one maybe the part four I don't know let's pick one in random and what this will need let's actually take a look at the model is it will need to have the upload file request model and this contains a file path and the file name the file path is the full path of the file in my drive I won't be making an interface that allows you to upload it because this will over complicate the thing but just so you know you can use a path or you can use an in-memory stream to do that and both ways are supported from client the other thing is a file name and this file name is what I want this to be named in blob storage so let's go to postman I a post request to adapt URL in fact let me check what the actual URL is is upload file so if I copy that and I paste it here they should allow me to call that endpoint and what I want this to look like from a calling standpoint is I'm gonna name this funky ng in the container but the path I'm gonna get from the full path on my crime and that is that one here and I'm gonna paste and I'll have to escape the back slashes and then I'm gonna upload the part for dot PNG and when I hit that assuming I implemented that method I will be able to upload that thumbnail in the container so let's see how we can do that let's let's implement that method and you'll see that it's very very simple the first thing you need to do again as always is you need a reference to the container client and in fact some of you might say that why don't you just inject the container client here instead of the service client yes you could also do that if you're only targeting one container I just wanted to make this generic enough so you can understand how you can change that to talk to something else so we have that here and what we need to do now in order to upload is we need to say perv or blob client so again we get a blob client to the get blob client of filename that we specified but you might say wait how did you get the blob client of something that doesn't exist yet well that's how it works you essentially get a reference to that blob but even if it doesn't exist you still have a reference to and you can do whatever you want with it you can upload to it you can delete it you can get it it's up to you and what we want to do is we want to upload something so we're gonna say a wait blob client in fact I should actually stop this application and turn this into an eye sinkhole so blood client dot upload async and first you can have a stream here you can have a path it's up to you extreme is mostly used for something that's in memory a path is for something that you want to load from your local drive I'm gonna show you how to do it with path but I'm also gonna show you how to do it with a stream in a second so don't worry about that and I'm gonna say file path file name file path which I get again from the user and I need to specify also the content type cause if I don't block that one doing data analysis and we just think that it's just a byte array essentially so for that reason I'm gonna say new blob HTTP headers and I'm gonna specify the content type to be equal to I'm gonna get the file path and I have an extension method called get content type and this extension method if I just f12 into it it will try to use the file extension content type provider that's built in dotnet core and will try to find the content type based on the extension if it's not something recognizable it will just use a different one so again that code is in the description you can find it if you want to use it and in order to upload it that's it you don't actually need to do anything else so let's take a look at that and let's see how it works so back in postman like I said this is the file I want to upload this is the path to it so i'm gonna click send and i'm getting okay back which is good so if i go back to the container and i refresh I can now see fam dot PNG here and if I go to edit you can see that the right thing is uploaded it's the benchmarking video so this is how you simply upload you can see the content type is also the right one which is great so let's just close that and let's go back to the code the next thing that we want to do is we want to be able to upload content from in memory essentially a stream and of course blob storage is not just about an image or a video for example if you have a login service that periodically pushes logs in to block storage those logs might look like a string for example I'm gonna get a JSON file for this example and I'm gonna create this station file in memory and let me just show you how I can do that I have a model here called upload content request and this content will be provided by the API from postman and it will create a file with the name that I provide to save in blob storage and we're going to be able to retrieve it as well so let's go back to blob storage in here I'm gonna get you guessed it two references a container client reference and a blob client reference so box standard yes a lot of repetition you could extract it I want to keep itself contact and what I need to do now is convert this string into a stream and not to do that I need to convert it into a bike array first so I'm gonna say var bytes equals and coding dot utf-8 dot get bytes of the content and then var memory stream equals new memory stream and in the constructor I'm gonna put the bytes that I want the stream to contain and something else I can do is I can use the using tag not like this just like this and this will know to clear the stream once it's out of context it's just a new she shops at you and what you need to do here is you need to say blob client dot upload actually I need to say I wait first dot block blob client doc upload async I use the memory stream and then again I'm gonna use the content type but this time I don't have a file path to get the type from so I'm gonna use the file name and hope that this actually makes sense and again I need to change this to a sink and change this to a sink using and change this to a weight using as well and this should be enough to upload the file so let's see so application is running and back in postman I'm gonna create a new tab and I'm gonna change the upload file to upload content which is the name of the endpoint and we're gonna copy that as it is so the body change it to Jason and the file path is content and in here I'm gonna content yeah that's more like it anything I'm gonna pay some Jason but we're gonna hand right so like test does it work can I need to escape the double quotes so let's just use back slashes for that and I'm going to call example dot J's and I'm gonna click send look at the type of this is upload content not upload content so I'm gonna send that it says okay so now we can go to the gate endpoint and instead of part one I'm gonna say example dot Jason so let's place that here and as you can see it works fine it has the right headers and the visualization is correct it knows it's an application Jason because it got the type from the extension and it returns it fine so this is how you upload something that it's created in memory essentially in blob storage the last thing I need to show you is of course how to delete something and in order to delete you guess there we're gonna get two references the container and the blob client and I'm gonna give the blob name here and that's how you essentially find the reference you use the blob name and then you say of course let me just turn this into an async and I say a wait blob client dot delete if exists async and that's it actually you don't actually need to do anything else this is it and let's just run this in order to test it so back to postman I'm gonna delete the example dot Jason I'm gonna use the HTTP delete so blobs what exactly the name of the blob line that's it okay cool so just paste that and send and it says okay so if I go to the content i refresh you see nothing and that's because it doesn't exist I deleted it and in fact let me just delete that as well the thumbnail back here paste it deleted boom it's gone I can keep sending it it's idempotent so you won't exactly know so it's gone and if i refresh you'll see that it's no longer there something else I should note is let me just go back to the content one if I go ahead and create this JSON file again and as you can see here it exists I can change something put some data and send it again and if I retrieve it it's now updated that's because creating and updating works in an absurd fashion so if something exists by default it will override it so be careful with that that's all I had for you for this video thank you very much for watching special thanks to my github sponsors for making these videos possible if you want to support me as well you're gonna find the link in the description down below leave a like if you like this video subscribe for more content like this and ring the bell as well you know to get notified when I upload a new episode and I'll see you in the next video keep coding
Info
Channel: Nick Chapsas
Views: 55,890
Rating: undefined out of 5
Keywords: Elfocrash, elfo, coding, asp.net, .netcore, dot net, core, C#, how to code, tutorial, asp.net core, csharp, rest, rest api, development, lesson, software engineering, dev, microsoft, .net core, nick chapsas, chapsas, asp.net core 3, azure, blob storage, queue storage, azure storage, blob storage beginners guide, azure tutorial, azure guide, dotnet, .net
Id: 9ZpMpf9dNDA
Channel Id: undefined
Length: 22min 48sec (1368 seconds)
Published: Tue Mar 10 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.