Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
  • Retrieve File from a private container in Azure

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 237
    Comment on it

    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.

    1. Azure Storage Account.
    2. Azure Storage Container.
    3. 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:

    1. AccountName(Storage Account Name)
    2. key(Key provided by Azure for account)
    3. BaseUri(Base Uri of storage Account)
    4. 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)

Sign In

Sign up using

Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: