Uploading files to Azure Storage using Asp.Net MVC

Showing some methods to be able upload and download file from Windows Azure Storage.

 Windows Azure   
 

Posted: October 25, 2017. | By: mustafa

Windows Azure cloud storage allows to store any files like text, images, video, etc. You could try using Azure storage services for free. Here I am going to show you some method to upload and download files from Azure Cloud Storage. 

Upload files to Azure Cloud Storage using Base64 String

To upload files as text to Azure storage we need to pass base64 string including content type from data file.

internal string UploadFromText(string textToUpload, string targetFileName, string ContentType)
 {
 string filePath = blobBaseUri + ContainerName + "/" + targetFileName;
 CloudBlockBlob blob = cloudBlobContainer.GetBlockBlobReference(targetFileName);
 blob.Properties.ContentType = ContentType; // set the content type of posted file.
 blob.UploadText(textToUpload);
 return filePath;
 }

internal FileContent DownloadText(string targetFileName)
 {
 FileContent file = new DataModel.FileContent();
 CloudBlockBlob blob = cloudBlobContainer.GetBlockBlobReference(targetFileName);
 //you have to fetch the attributes to read the length
 blob.FetchAttributes();
 file.ContentType = blob.Properties.ContentType;
 file.FileString = blob.DownloadText();
 return file;
 }

 

Upload files to Azure Cloud Storage using Byte Array

 internal string UploadFromByteArray(Byte[] uploadBytes, string targetFileName)
 {
 string status = string.Empty;
 CloudBlockBlob blob = cloudBlobContainer.GetBlockBlobReference(targetFileName);
 blob.UploadFromByteArray(uploadBytes, 0, uploadBytes.Length);
 status = "Uploaded byte array successfully.";
 return status;
 }

 internal Byte[] DownloadToByteArray(string targetFileName)
 {
 CloudBlockBlob blob = cloudBlobContainer.GetBlockBlobReference(targetFileName);
 //you have to fetch the attributes to read the length
 blob.FetchAttributes();
 long fileByteLength = blob.Properties.Length;
 Byte[] myByteArray = new Byte[fileByteLength];
 blob.DownloadToByteArray(myByteArray, 0);
 return myByteArray;
 }

 

Upload files to Azure Cloud Storage using stream

While downloading file from cloud storage DownloadToStream method save the stream from blob file to same object passed in the method. And we return the status 'Download successfully'.

internal string UploadFromStream(Stream stream, string targetBlobName)
 {
 string filePath = blobBaseUri + ContainerName + "/" + targetBlobName;
 //reset the stream back to its starting point (no partial saves)
 stream.Position = 0;
 CloudBlockBlob blob = cloudBlobContainer.GetBlockBlobReference(targetBlobName);
 blob.UploadFromStream(stream);
 return filePath;
 }

internal string DownloadToStream(string sourceBlobName, Stream stream)
 {
 string status = string.Empty;
 CloudBlockBlob blob = cloudBlobContainer.GetBlockBlobReference(sourceBlobName);
 blob.DownloadToStream(stream);
 status = "Downloaded successfully";
 return status;
 } 

 

Download file to local folder path from the Windows Azure cloud storage.

internal string DownloadFile(string blobName, string downloadFolder)
 {
 string status = string.Empty;
 CloudBlockBlob blobSource = cloudBlobContainer.GetBlockBlobReference(blobName);
 if (blobSource.Exists())
 {
 //blob storage uses forward slashes, windows uses backward slashes; do a replace
 // so localPath will be right
 string localPath = Path.Combine(downloadFolder, blobSource.Name.Replace(@"/", @"\"));
 //if the directory path matching the "folders" in the blob name don't exist, create them
 string dirPath = Path.GetDirectoryName(localPath);
 if (!Directory.Exists(localPath))
 {
 Directory.CreateDirectory(dirPath);
 }
 blobSource.DownloadToFile(localPath, FileMode.Create);
 }
 status = "Downloaded file.";
 return status;
 }

 

Finally a method to delete a file from Azure blob storage

 internal bool DeleteBlob(string blobName)
 {
 bool status = false;
 CloudBlockBlob blobSource = cloudBlobContainer.GetBlockBlobReference(blobName);
 bool blobExisted = blobSource.DeleteIfExists();
 return blobExisted;
 }

related posts

Back to top