Now in the current scenario, Most of the Project Owners want to keep their data on cloud. Specially for the .Net projects, we often need to store data in Cloud based Microsoft Azure server.
Azure provides azure storage account to facilitate the storing of files and images in Azure Blobs.
In Azure, we have three important things for storing files.
- Azure Storage Account.
- Azure Storage Container.
- Azure Blob.
Container is contained within a storage account and Blobs are contained within the Container.
In order to retrieve files from Azure container we must have these 4 information:
- AccountName(Storage Account Name)
- key(Key provided by Azure for account)
- BaseUri(Base Uri of storage Account)
- ContainerName(Name of container)
Once we have all above information, we can store and retrieve files on/from azure
For private containers we need to authenticate through user credentials while for public containers, files can be accessed through the url.
Below mentioned is the code to retrieve a file from Azure Blob.
public byte[] GetUserFile(string filePath)
{
byte[] byteArray = null;
StorageCredentialsAccountAndKey creds = new StorageCredentialsAccountAndKey(accountName, key);
CloudBlobClient blobStorage = new CloudBlobClient(baseUri, creds);
CloudBlobContainer container = blobStorage.GetContainerReference(containerAddress);
CloudBlob Blob = container.GetBlobReference(filePath);
BlobStream blobStream = null;
blobStream = Blob.OpenRead();
blobStream.Seek(0, SeekOrigin.Begin);
byteArray = new byte[blobStream.Length];
blobStream.Read(byteArray, 0, byteArray.Count());
return byteArray;
}
In the above written function, We are trying to retrieve byte array from Azure blob using the blobName(File Name).
This function returns the byte array of the blob where byte array is stored.
0 Comment(s)