Uploading Stream in AZURE BLOB

In this post I will show you to upload stream to Azure BLOB.

Set the Connection String as below,

image

 

Function is as below,

 


public void UploadBlob(Stream s, string fileName, string containerName, string connectionString )
        {
            account = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue(connectionString));
            blobClient = account.CreateCloudBlobClient();
            container = blobClient.GetContainerReference(containerName);
            blob = container.GetBlobReference(fileName);

            s.Seek(0, SeekOrigin.Begin);
            // set the required content type
            blob.Properties.ContentType = "image/jpeg";
            blob.SetProperties();
            BlobRequestOptions options = new BlobRequestOptions();
            options.AccessCondition = AccessCondition.None;
            blob.UploadFromStream(s, options);

        }


You need to set required content type. If you are uploading image then content type would be like below,

 

image

To use this function you need to pass ,

  1. Public container name
  2. BLOB name as filename
  3. Data connection string

I hope this quick code snippet was useful. Thanks for reading. Smile


Discover more from Dhananjay Kumar

Subscribe to get the latest posts sent to your email.

Published by Dhananjay Kumar

Dhananjay Kumar is founder of NomadCoder and ng-India

4 thoughts on “Uploading Stream in AZURE BLOB

  1. what prompted you to use blob.UploadFromStream(s, options) and not blob.UploadFromStream(s) ?
    did you find that overload is better in any way than simple UploadFromStream(stream)?

Leave a comment

Discover more from Dhananjay Kumar

Subscribe now to keep reading and get access to the full archive.

Continue reading