In this video, Sharon demonstrates how to create an Azure Resource Group, storage account, container, and Blob using PowerShell. Uploading and downloading files using PowerShell are also demonstrated.
- [Instructor] In the previous lesson, I showed you how to create an Azure storage account using the portal. Now let's do the same thing, but we're going to use PowerShell instead. As you can see, I already have a script up for us to use, and I have logged into Azure already. Let's start stepping through this script. The first thing you'll notice is I have three variables already pre-defined for us. We have the resource group name, RG name, location, and account name. Instead of running this script through all the way, we're going to step through it line by line.
First thing we're going to do is create a new Azure resource group using New-AzureRmResourceGroup, providing the name, and this will be the variable information from our variables. And our location. But before I do that, I better go ahead and run the variables. There we go, now we can continue on. I'm going to go ahead, run this line. And we can see here that our resource group has been created. Our next line is to create the new Azure RM storage account itself.
We need to include the resource group name, again we're going to use our variable. You can now see the benefit of using variables, because I don't have to retype this over and over and over again. We're going to provide the account name, the location, and the type of storage account. In our example here, we're using a standard LRS. I'm going to go ahead and run this line. We can now see that we have a storage account name called LIL Storage Account.
I'm going to scroll up a little bit, give us a little bit more real estate to work with. Next, in order to create a container, we'll need the storage key for that account. And that is exactly what our next line does. It stores the storage key in the variable storagekey. We'll use the command or cmdlet Get-AzureRmStorageAccountKey, we'll provide the resource group name and the account name. I'm going to go ahead and run that. Perfect, that pulled that information for us and then stored it in that variable.
Now we need the context variable, which is created when we create the new storage context. And we'll need to pass the storage account key this time, as well. Here, our command is New-AzureStorageContext, again providing the storage account name and the storage account key. Now, one thing I do want to point out here is you'll notice that we're using the variable storagekey from the line we just ran, with a dot value bracket zero and bracket. What this will do is pull the value of key one.
Now, depending on what version of PowerShell you're using, this may be slightly different, so keep that in mind. And finally, we're going to end this with a protocol of HTTP. I can go ahead and run this line. That information has now been stored in the variable Context. I'm going to go ahead and scroll up a little bit here so we can see our next line. And we can now finally create the Azure storage container, specifying the name.
And here is our name here, it's called Files. Now, I could have easily done this from a variable as well, but I've decided to be a little bit different as I work through the script. We'll assign the permission that we want to assign to our container, and finally, we need to pass the context variable. So let's go ahead and run this line. And you'll notice I have an error here. And the reason I have the error is because I'd highlighted the word files. So that was my mistake, so I'm just going to go ahead and rerun that selection one more time.
And there we go. And notice now that we have a container called files. Fantastic. Let's go ahead and upload some files to our new container. So I'll scroll down in our script a little bit, and when you'll notice that our command to upload files is get-AzureRmStorageAccount. Our resource group name, again, we're providing that variable as well as the account name, and then we're piping that through to set Azure storage blob content specifying our container, which we've just called Files.
And then next you need to add the path for those files. And don't forget to include the filename. I can go ahead and run that, and you'll see that our file was uploaded very quickly. This is a blank file. And there we go, we can see that we now have File1.txt. And finally our last step is to download files. Again, we need the storage account key, because we need access to that container.
So our first line is to get the Azure account key and we're going to use the command get-AzureRMStorageAccountKey, specifying the resource group and the account name, and we're storing that in the variable storagekey. I'm going to go ahead and run that line. And then this context variable line should look very familiar at this point. All we're going to do, again, is use NewAzureStorageContext, provide the storage account name and the storage key, again specifying Key1 in this case.
Specifying the filename that we want to download. Finally, our last line, get Azure Blob Content Blob Filename, which we provided, was filename text. The container comes from files, and then our destination. Now, in this case here, I'm actually expecting a prompt to show up. Perfect.
And the reason I have this prompt is because if you remember, in our previous steps, we uploaded a file called File1 to Azure. Now we're downloading that same file with the same filename. So now I'm prompted, do I want to overwrite this? I'm going to go ahead and say yes. Go ahead and we'll just quickly take a look at that folder. Unfortunately, I hit Enter instead of Tab there, so let me continue and put in the full path name.
I'm going to go ahead and take a look, and you'll notice here that we have the Azure storage script that is available, plus we have the File1, and we can see that was just recently downloaded. As you can see, it was very easy to create the Azure storage account using PowerShell, upload and download files to the containers. If you haven't had the chance to really play with PowerShell as of yet, I'd highly recommend that you do so, it's really not that bad. And if I can do it, anybody can do it.
- Designing data storage
- Azure Blob storage
- Creating Blob storage using PowerShell
- Azure Cosmos DB
- Securing Azure SQL Database
- Selecting the appropriate storage option
- Virtual machine storage tiers
- Managed vs. unmanaged disks