Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Store File in Azure Blob

    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 303
    Comment on it

    As i have already defined what all things we need as prerequisite to access a blob in Azure in my previous blog Retrieve File from a private container in Azure, Here is the code for Storing byte array in an Azure blob

    public string UploadByteArrayToAzureBlob(byte[] imageBytes, string type, string extension)
            {
                if (imageBytes == null)
                {                
                    return string.Empty;
                }    
                try
                {
                    Guid imagename = Guid.NewGuid();
                    StorageCredentialsAccountAndKey creds = new StorageCredentialsAccountAndKey(accountName, key);
                    CloudStorageAccount account = new CloudStorageAccount(creds, false);
                    CloudBlobClient blobStorage = new CloudBlobClient(baseUri, creds);
                    CloudBlobContainer objContainer = blobStorage.GetContainerReference(containerAddress);
                    objContainer.CreateIfNotExist(); //Checks if Container exist or not. 
                    CloudBlob obj = objContainer.GetBlobReference(string.Format("{0}_{1}.{2}", type, Convert.ToString(imagename), extension));
    
                    obj.Metadata["MetaName"] = "meta";
                    // Open a stream using the cloud object
                    BlobStream blobstream = obj.OpenWrite();
                    // Write the stream to the blob database
                    blobstream.Write(imageBytes, 0, imageBytes.Count());
                    blobstream.Close();
    
                    return string.Format("{0}_{1}.{2}", type, Convert.ToString(imagename), extension);
                }
                catch 
                {
                    return string.Empty;
                }
    
            }
    

    In the above written function, We are trying to upload a byte array to Azure blob. Type is the type of file. i.e. ProfilePic Extension is the extension of file. i.e. zip,jpg This function returns the file name of the blob where byte array will be stored. this file name will be used for retrieving the image back from blob.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
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: