Introduction
Azure blob storage is an object storage solution in the cloud that stores unstructured data (include audio, video, and text, etc.) to the Azure platform. This data can be accessed from anywhere in the world. Blobs are grouped into "containers" that are tied to user accounts.
Blob storage is designed for saving images to a browser, writing to log files, backup/restore, analysis of stored data, etc.
This article is divided into two parts. The first part is to create Azure blob storage in the Azure portal with security and then write .net core to push and pull the data from blob storage.
Create Blob Storage
Step 1
Go to the
Azure Portal and create a new resource -> Storage -> Storage Account.
Step 2
Enter all the details like unique storage account name, account type, and replication i.e. replica of one data center to another data center. You can select all other parameters as default for this example.
Step 3
Create a new container under the storage account for upload the file in container. select the access level as container(public) then later will change to private to see how to access secure data.
Step 4
Upload the image from your machine into Azure by using the upload option.
Click on the image (jpg) file and copy the URL and open in a new tab, you should find a URL similar to below one. Anyone in the world can access this URL. https://myblobstoragefirst.blob.core.windows.net/images/MicrosoftAzure-logo.jpg lets move to add security on this container by changing the access level to private.
Create Azure blob storage with security (SAS's)
To use the security, change the access level to private and check the URL again, you may get the below error message when you try to access the same URL created in the previous step.
The specified resource does not exist. RequestId:a4644a6c-801e-001a-47e9-a22575000000 Time:2019-11-24T17:04:30.1733108Z.
Let's create a SAS token to access the same image. Right-click on image and Generate a SAS.
you may notcied that blob SAS URL is generated, you can take this new url to acess the same image.
Managing blob storage by .NET
Once your blob account is created, now you can push and pull the data from this account. Let's have a new .NET project to do it.
Step 1
Create a new .netframework console application to test this.
Step 2
Import the Nuget packages Microsoft.Azure.KeyVault.Core & Microsoft.WindowsAzure.ConfigurationManager to access Azure storage account services.
Step 3
Perform the same steps using code as we did in the Azure portal. Create a Storage account first, then a container and download the image. Here is the code:
- static void Main(string[] args)
- {
- CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnection"));
- CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
- CloudBlobContainer container = blobClient.GetContainerReference("newimages");
- container.CreateIfNotExists(BlobContainerPublicAccessType.Blob);
-
-
- CloudBlockBlob blockBlob2 = container.GetBlockBlobReference("Screenshot.png");
- using (var fileStream = System.IO.File.OpenWrite(@"path to download with imagefile and extension"))
- {
- blockBlob2.DownloadToStream(fileStream);
- }
- }
Step 4
Once the download image is done, then you can upload the image to the storage account from your host machine.
-
- CloudBlockBlob blockBlob = container.GetBlockBlobReference("img2.jpeg");
- using (var fileStream = System.IO.File.OpenRead(@"path of file in local directory"))
- {
- blockBlob.UploadFromStream(fileStream);
- }
Conclusion
Here, we experienced Azure blob storage account with shared access signatures(SASs)with the Azure portal and .NET language.