C# code to push multiple files from local disk to blob container

Rajnilari2015
Posted by Rajnilari2015 under Azure category on | Points: 40 | Views : 341
The below code will do so

private void PushToBlob()
{
string folderPath = ConfigurationManager.AppSettings["sourceFolder"];
string connectionString = CloudConfigurationManager.GetSetting("StorageConnectionString");
string containerName = ConfigurationManager.AppSettings["containerName"];
string blobName = ConfigurationManager.AppSettings["blobName"];
var accountFiles = Getfiles(folderPath);

//BlobStorageUtility df = new BlobStorageUtility();
var blobContainer = GetCloudBlobContainer(connectionString, containerName);

foreach (var accountFile in accountFiles)
{
var blob = blobContainer.GetBlockBlobReference(Path.GetFileName(accountFile));
using (var fileStream = File.OpenRead(accountFile))
{
blob.UploadFromStream(fileStream);
}
}
}

//These are auxilary functions

private List<string> Getfiles(string folderPath)
{
return Directory.GetFiles(folderPath).ToList();
}

private CloudBlobContainer GetCloudBlobContainer(string connectionString, string containerName)
{
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
CloudBlobClient blobclient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer blobcontainer = blobclient.GetContainerReference(containerName);

if (blobcontainer.CreateIfNotExists())
{
blobcontainer.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });
}
return blobcontainer;
}

Comments or Responses

Login to post response