Add And Obtain Information From Blob Storage Utilizing C#
That is the second a part of the Introduction to Azure Blob storage. Right here we are going to see entry the Azure weblog storage for importing and downloading recordsdata utilizing C#.
When you should not have a storage account, please learn the primary a part of this text to create an Azure storage account.
You’ll find the primary half right here >> Create an Azure Storage account and a storage container for blob storage
As soon as we have now created the Azure storage account and container let’s develop a easy C# console utility to add and obtain the recordsdata to the blob storage programmatically.
First, create a C# console utility.
To entry the Azure storage account we have to set up the NuGet package deal WindowsAzure.Storage.
I’ve put in the most recent model v9.3.3.
After the package deal has been put in, we have to embrace the next references in our utility.
utilizing Microsoft.Azure;
utilizing Microsoft.WindowsAzure.Storage;
utilizing Microsoft.WindowsAzure.Storage.Blob;
In the principle technique, I’ve created 2 strategies
- Upload_ToBlob(local_file_Path, Azure_container_Name): To add the file to the Blob storage
- Download_FromBlob(filename_with_Extention, Azure_container_Name): To obtain the file from the Blob storage
Please confer with the code snippet beneath.
static void Foremost(string[] args)
{
AzureBlobSample azureBlob = new AzureBlobSample();
azureBlob.upload_ToBlob("D:data_d.txt", "mydatacontainer");
azureBlob.download_FromBlob("data_d.txt", "mydatacontainer");
Console.ReadKey();
}
Add recordsdata to the Blob storage
The strategy definition for the Upload_ToBlob() is given beneath.
Right here I’m utilizing 2 parameters for the tactic.
One for the file title and the opposite for the Azure container title.
public void upload_ToBlob(string fileToUpload, string azure_ContainerName)
{
Console.WriteLine("Inside add technique");
string file_extension, filename_withExtension, storageAccount_connectionString;
Stream file;
// Copy the storage account connection string from Azure portal
storageAccount_connectionString = "your Azure storage account connection string right here";
// Studying the file as filestream from native machine
file = new FileStream(fileToUpload, FileMode.Open);
CloudStorageAccount mycloudStorageAccount = CloudStorageAccount.Parse(storageAccount_connectionString);
CloudBlobClient blobClient = mycloudStorageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference(azure_ContainerName);
// Checking if the container exists or not
if (container.CreateIfNotExists())
{
container.SetPermissionsAsync(new BlobContainerPermissions
{
PublicAccess = BlobContainerPublicAccessType.Blob
});
}
// Studying file title and file extension
file_extension = Path.GetExtension(fileToUpload);
filename_withExtension = Path.GetFileName(fileToUpload);
CloudBlockBlob cloudBlockBlob = container.GetBlockBlobReference(filename_withExtension);
cloudBlockBlob.Properties.ContentType = file_extension;
cloudBlockBlob.UploadFromStreamAsync(file); // Importing the file to the blob
Console.WriteLine("Add Accomplished!");
}
Within the above technique, we copied the storage account connection string from the Azure portal.
Azure can discover the connection string by clicking the Entry Keys menu within the Settings part.
Please confer with the screenshot beneath.
Copy the connection string and assign it to the storageAccount_connectionString variable within the upload_ToBlob() technique.
storageAccount_connectionString = "your Azure storage account connection string";
The beneath 3 statements are used to create the objects for the Storage Account, Blob consumer, and Blob container.
CloudStorageAccount mycloudStorageAccount = CloudStorageAccount.Parse(storageAccount_connectionString);
CloudBlobClient blobClient = mycloudStorageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference(azure_ContainerName);
The beneath code snippet checks whether or not the required container exists in storage a, account, or not.
If it exists, the appliance will use the present container. In any other case, it can create a container contained in the storage account with a specified title.
if (container.CreateIfNotExists())
{
container.SetPermissionsAsync(new BlobContainerPermissions
{
PublicAccess = BlobContainerPublicAccessType.Blob
});
}
The beneath assertion is used to create a Block blob object utilizing the filthe e title with extension.a
CloudBlockBlob cloudBlockBlob = container.GetBlockBlobReference(filename_withExtension);
cloudBlockBlob.UploadFromStreamAsync(file) assertion is used to add the file to the blob storage.
Obtain recordsdata from the Blob storage
In my implementation, I’ve used 2 parameters for the download_FromBlob() technique.
One is the File Identify and the opposite one is the Azure container title.
The definition for the download_FromBlob() technique is given beneath.
public void download_FromBlob(string filetoDownload, string azure_ContainerName) {
Console.WriteLine("Inside downloadfromBlob()");
string storageAccount_connectionString = "Paste you storage account connection string right here";
CloudStorageAccount mycloudStorageAccount = CloudStorageAccount.Parse(storageAccount_connectionString);
CloudBlobClient blobClient = mycloudStorageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference(azure_ContainerName);
CloudBlockBlob cloudBlockBlob = container.GetBlockBlobReference(filetoDownload);
// present the file obtain location beneath
Stream file = File.OpenWrite(@"E:ToolsBlobFile" + filetoDownload);
cloudBlockBlob.DownloadToStream(file);
Console.WriteLine("Obtain accomplished!");
}
The codes are the identical because the add technique.
cloudBlockBlob.DownloadToStream(file) assertion is used to obtain, and add the file from the blob storage.
Since it’s a primary utility, I haven’t used any validation to examine whether or not the file and the container exist or not.
You’ll be able to add extra logic to make the appliance safer and correct.
Completely happy Coding
Know extra about our firm at Skrots. Know extra about our companies at Skrots Companies, Additionally checkout all different blogs at Weblog at Skrots