BrandGhost

Azure Blob Storage - Read and Write Within MINUTES!

You might have heard of blobs in computing -- but no, they don't have anything to do with one of our favorite blob Pokemon! Blobs are Binary Large OBjects and there are cloud services available to allow us to read and write data. In this tutorial, I'll show you how to catch a Ditto. I mean... How to go through the basics of reading and writing blobs from a simple CSharp application!
View Transcript
today we're going to be talking about blob storage and no I don't mean some container that you put all of your Ditto Pokémon into I mean being able to store binary large objects in the cloud hi my name is Nick centino and I'm a principal software engineering manager at Microsoft in this video we're going to go over an introduction to getting Azure blob storage set up and I'm going to walk through a little bit of the setup that you'll see when you're in the cloud portal and then from there we're going to look at a very simple C program that will let us do some of the basics using Azure blob storage if that sounds interesting just a reminder to subscribe to the channel and check out that pin comment for my courses on dome train now let's look at some of the setup that we have to do in the Azure portal all right I've gone ahead and I've taken screenshots of the various steps that will go through to create our storage account so in order to get to this screen if you're looking at the Azure services that you have available you can even search for blob storage or storage accounts you'll notice rate at the top this is the storage accounts classic view and then what you'll want to do is select your subscription so I'm using visual Studio Enterprise subscription for mine and then a resource Group and you can create a new one if you haven't done any of this before from there we're going to give it a name I've just called my Dev leader test and we're picking a region and I should call out that as I'm walking you through this I'm not going to be telling you the most ideal settings that you need to be using in your situation because quite simply I don't know your situation and that means that you're going to want to consider all of these settings that we're going to walk through situationally consider what you have going on in your current setup maybe consult with your team and just don't follow everything blindly the service that I'm selecting here is the Azure blob storage and then I'm just having mine as backup an archive here but depending on your needs there's other modes that you can select here like if you need to have more hot access so the idea is that you're picking between stuff that you want to store and maybe not access very much and on the other end of the spectrum you need to be accessing it more frequently there are different costs associated with this at the bottom here we just have some performance and redundancy settings so I'm not going to spend much time on those because again these are things that you're going to want to consider in your own setup on this screen something that I just want to focus on up at the top here because this is a bit of a trap that you can fall into if you're trying to get things set up when it comes to being able to connect to your blob storage we do want to make sure that you're doing things in a secure way there is this setting here that says allow enabling Anonymous access on individual containers this is by default turned off you probably want to keep it this way for most of the scenarios that you can think of but I just wanted to mentioned that if you're having difficulty getting connected I would try to avoid just turning off security measures instead I would invest the time go doing a little bit more research and figuring out where your problems occurring because at the end of the day if you have it all functional but it only worked because you turned off all of the security measures you're probably opening up yourself to some vulnerabilities another thing to note on this screen is at the bottom I was talking on the previous screen about hot versus cool and you'll see that you have that option down here to kind of alternate between those two modes I am going to be taking a little bit of a shortcut on this third step and I am selecting enable Public Access from all networks again this is something that if you're thinking about how this is going to work in reality if we have blob storage as a service in the cloud odds are we're probably also running something else in Azure like a server and we want to be able to communicate with blob storage you don't need to be able to open up public access to be able to do that instead you can keep it on the same network in the cloud and basically have that internal to your Cloud setup and that way you can have your service communicating with blob storage directly without going through the public internet by opening things up to the public you create a whole bunch of different vulnerability scenarios you have more surface area for people to be able to access and work with the services you have so I would highly recommend not using this first setting but in my situation just to keep things very simple for this demo I do have that checked off I just wanted to be able to explain that so that when you're trying to make this decision maybe if you're rapidly prototyping something and just trying to get it working you can consider that but in a production scenario it's probably not something that you want to use on this screen I just left everything as all the defaults that you see here but I wanted to point out that there is a concept of soft deleting both blobs and the containers that is an option that you can play around with something that you might want to consider for your use cases from here this is where I just press the review and create button at the bottom of the screen and then We're Off to the Races so we can jump over over to some C code but there is one more thing that we're going to want to access before we start writing code and that is that if we go to this access Keys selection here on the left so that's under the security and networking tab once your blob storage account has been created you can go to security networking and then access keys and we are going to want to take one of these connection strings we're going to leverage that string in our code coming up in just a moment don't forget you're going to want to copy this connection string because without this it's going to be very difficult to make a good connection to your blob storage you're probably going to get stuck right at the beginning without this before we move on this is just a quick reminder that I do have a course on C refactoring available on dome train refactoring is one of the most critical skills you can learn as a software engineer and this helps you continue to build upon applications that already exist making sure that they can scale and have extensibility I walk you through a bunch of various techniques and give you some examples that we walk through together to see how we can apply these techniques to Factor the code check out the pin comment and the links in the description to get this course now back to the video heading over to visual studio now the one n get package that I'm going to be using is azure storage blobs this is at version 12.22 depending on when you're watching this there may be new versions ideally the apis and stuff have not changed or if they have very little so hopefully all of this conceptually still makes sense so that's the only Nate package I'm working with and in this example we're just dealing with a console application so I do have a lot of commented code you can kind of scroll with your eyes a little bit ahead but I'm going to be walking through different ways that we can start by creating a container then we're going to be creating blobs deleting them reading them all that kind of stuff coming up in just a moment so to kick things off we do need to have a blob service client and that's what I have declared right at the top here so lines 3 through 11 and what I've commented out or kind of hidden behind a region here it's not commented but it says top secret stuff that's that connection string that I took off of the previous page that I was just walking you through where I said don't forget to copy this what you're going to want to do is use the URI class so the way that you'll want to think about this is that inside of top secret stuff I really just have new URI and then the connection string that we copied from the portal this you could declare above and just pass connection string into here if you pass it in as just a string because that's what you're thinking the type needs to be it won't work you'll probably get a couple of errors but if you wrap it in new URI then it should all work as you might expect now that we have a blob service client we can start communicating with Azure blob storage but what do we want to do from there in Azure blob storage once we have our storage account and we have the service client what we need to be able to do is start interacting with blob containers at this point we've made the Storage account but we don't have any containers inside of that storage account so if we look through lines 14 through 16 here this is where we can either create or access a container I'm going to take this line and comment it out I'm going to put in this first line on 14 it will be very similar to line 15 so if we have the blob service client we can say create blob container and give it a name and there are restrictions around the name by the way so you'll want to look into this in the Azure documentation I believe it's just all lowercase characters so you can't use special characters or underscores or capitals or spaces so something really simple like this and the return value of this is that we're going to get a blob container client that's going to allow us to do some more work following this but I did want to point out that if you use the blob service client then you can ask to create a container client and then ask it to create the container itself so one is making a container so in this step up here it's making both the container in Azure storage as well as giving you back a reference to this blob container client instead you can break this out into multiple lines and create the client itself to do the work and then ask that client to create the container so I'm going to do it all in one line so if we were to go run this we should be able to get a container so let me go ahead and run it okay and nothing too exciting because we're not printing anything out to the console but if we look at line 15 if I go back to the code here what I wanted to do let's comment this back up and now we should be able to get that container right so that means for the rest of the code we can go check to see that we actually have that container created because if we go to access it it would give us an error if it wasn't already made if we go run this now if we see no exceptions it should mean that we have access to the container which is great again just for kind of a mental model here we have Azure blob storage as a service we made a storage account so it's going to be the top level part and then we made a container in this case very creatively named the container so now we have the container that we can start working with what I want to do from here is we're going to start off by making our first file so what I want to do online 17 is we're going to ask the container client to give us a blob client you'll notice that I have like a path here and the way that blob storage works is technically you don't really have the concepts of files and folders but the way that you can put things into the path is that you can make it feel like that and even in Azure in the portal you can actually see that it will represent these things as folders and things for you that doesn't really have anything to do with the underlying uh technology right so this is just a unique identifier for The Blob that we're going to be working with putting a slasher there makes it look and feel like you're dealing with a file folder hierarchy so if we do this we should have access to a blob but what I'm going to do if we look at the following lines is I'm actually going to write hello world into there so let's go ahead and run this and see if we get what we need in the portal okay so no errors it successfully wrote things out but let's go prove it all right so I've gone into my Dev leader test storage account and I've clicked on the container that was made so that's here in the left nav navigation and we can see that I have folder name so that's going to be the first part of that path segment that I was talking about but if I click into that we can see that we have test.txt so this did make a blob it's 13 bytes long and what do we think is inside of this if I use the three dots on the far right hand side I can go to view/edit and when I go to do that we can see the blob is the full sort of identifier kind of looks like a path to us and we can see that truly it does have hello world inside of it so we were able to go write this blob now jumping back to the code I'm going to comment out the part that did the writing instead we're going to read things back so I'm going to use the same uh blob identifier again it looks like a path to us I'm going to ask to read it in and then we're going to write it to the console hopefully we see Hello World cuz we just saw that in the portal itself there we go hello world so This Is Us accessing that blob and reading those bites back I wanted to talk about a few more things that you can start to play around with as you're getting more familiar with blog storage so in this case we're going to get a blob client to the same path that we were just talking about but there's other ways that you can play around with putting data there for example instead of just having in this case uh back up at the top where I had a streamwriter that was writing text into that blob instead what you might want to do is upload a file path so you might have a local file and you just want to put that entire file to where the blob is so you could use this one right here on line 29 this parameter is a file path locally and if instead you want to go use a stream you could go open up a file stream pass that in you could use a network stream you could use any type of stream and this upload a sync will go read from that stream and write those bytes to the blog so you have a bunch of different options to get data from wherever as long as you have a file path or a stream you can start sending those bytes to blob storage something else that we can play around with is metadata so the more that you're getting comfortable with using Azure blob storage and you might want to be able to have metadata associated with the blobs that you have there we do have options to be able to set metadata on there so this is just going to be a dictionary of keys and values so if we go ahead and run this we should be able to set the metadata and check it in the Azure portal okay so this ran with no exceptions let's go check it out in the portal now I've gone ahead and pressed the refresh button then what I'm going to do is just go to properties from that three dot menu when I pull that up we should be able ble to see if I scroll down a little bit that metadata section down here now has the keys and values that we just set in the code okay the final thing that we're going to do here is we're going to take our blob client and we're going to delete that blob so we go blob client and if I say delete and we can do delete a sync there's an if exists uh and there's just policies that you can delete as well so the interesting thing about deletes that we need to talk a little bit about is this idea of snapshots so with blob store storage you can have snapshots if you say that you want to delete you need to actually delete all of the snapshots as well if you truly want that file to be deleted so I'm going to use delete if exists because it does and there's an async version there we go and then the first thing it's a kind of a long parameter here so I'm going to try to make this fit give me one moment here we'll get that trimmed up I just need to have the using statement at the top and I'm going to say incl include snapshots so we don't have any snapshots but again if there were snapshots we want to delete all of the blob snapshots the different versions of that blob therefore it will delete the entire blob as we would expect so I'll put my semicolon there I'm going to go ahead and run this and then we'll go check the portal okay so no exceptions when we run this let's head back to the Azure portal now when I go press refresh here let's see what happens and there we go so there's no file in here but if I go back up to the container you'll also notice that there is no folder right that might not be what you expected and that's why I wanted to show you this example because the folder itself was not an entity of any kind that we created it was only sort of conceptually there because the way that we named our blob that identifier we gave it seemed to have like a file folder structure so that's a really important concept that I want to point out here you don't have to use like file path style naming conventions you you can just give things goids or give them string or integer style names you could do whatever you want but if you happen to use something that looks like a file folder structure you'll see it show up here but the folders are not real things they're just kind of inferred so I hope that makes sense it's a little bit weird if it's not what you're expecting because if you thought that I made a directory and I made a file that's not what happened I only made the file once we deleted that file and there was no other sort of Association with that uh sort of uh derived directory it's not a real thing there's no reason for Azure blob storage in the portal to show it because it never existed in the first place so in this video we've looked at some of the basics of working with Azure blob storage but I wanted to touch on something that's a little bit interesting if you're building web applications in this example we were talking about writing some server side code most likely where you want to be able to upload and read back stuff from blob storage however what happens if you have something like a back-end server and a front end and you want to allow your users in the front end to be able to upload files to Azure blob storage or what if you want your users to be able to download stuff to the browser to be able to use in your web application do you need to be able to go from the front end through your backend server to blob storage and then from blob storage back to your server to the front end or is there a different way that we can do this to not have to stream the bytes all the way through well when that video is ready you can check it out right here thanks and I'll see you next time

Frequently Asked Questions

What is Azure Blob Storage and how is it different from traditional file storage?

Azure Blob Storage allows you to store binary large objects in the cloud, which is different from traditional file storage because it is designed for unstructured data and can handle large amounts of data efficiently.

How do I securely connect to my Azure Blob Storage account?

To securely connect to your Azure Blob Storage account, I recommend using the connection string found in the access keys section of your storage account. Avoid enabling anonymous access and instead ensure that your services communicate over a secure network.

Can I delete a blob and its snapshots in Azure Blob Storage?

Yes, you can delete a blob and its snapshots by using the delete method with the option to include snapshots. This ensures that all versions of the blob are removed, not just the current one.

These FAQs were generated by AI from the video transcript.
An error has occurred. This application may no longer respond until reloaded. Reload