Download File and Upload to Azure Blob Storage

Let’s say we have requirement to download file from website and upload it to Azure Blob Storage. In our case we are using excel file but you can do it for other files as well.

First we need to setup Storage account and blob container in Azure. Once you have setup it, you need to get following details:

> Connection String: You can navigate to storage account and get connection string like below
> Blob Container Name: You need to get name of the blob container
> File Name: Name of the file that you want use for storing

Once we have all above details we can use following code. First we will download data to byte array using Web Client

Uri _url= new Uri(@"URL of the location from where you want to download file");
var _webclient = new WebClient();
//in case you need to pass credentials to website
_webclient.Credentials = new NetworkCredential("Username", "Password");
byte[] _filedata = _webclient.DownloadData(uri);

//Once file is downloaded to byte array we can upload file to Azure Blob Storage
 var _storageaccount = CloudStorageAccount.Parse("Your Azure Storage Account Connection String");
 var _cloudblobclient = _storageaccount.CreateCloudBlobClient();
 var _cloudblobcontainer = _cloudblobclient.GetContainerReference("BlobContainerName");
 var _filename = _cloudblobcontainer.GetBlockBlobReference("Filename.xlsx");
//setup mime type
 _filename.Properties.ContentType = System.Net.Mime.MediaTypeNames.Application.Octet;

  if (!_filename.Exists())
                using (Stream stream = new MemoryStream(_filedata))
                    await _filename.UploadFromStreamAsync(stream);

Check our other blogs for Azure
Developing Scheduler for Dynamics 365 CE using Azure Function Part 1
Moving Azure Function Configuration – Quick Tip
Dynamics 365 CE and Azure Function
Cannot connect to your Azure SQL DB logical server- Azure SQL Server
Using Configuration with Azure Function
Using App User in KingswaySoft Connection Manager

Hope it will help someone !!
Keep learning and Keep Sharing !!

2 thoughts on “Download File and Upload to Azure Blob Storage

  1. necsar

    Your subscribe link not working. I got always error warning.
    “There was an error when subscribing. Please try again.” Please can you check it.


Leave a Reply

Your email address will not be published. Required fields are marked *