Advertisements

Archive

Posts Tagged ‘Tool to browse/upload files to azure drive’

How to upload blob to azure using ASP.Net / Tool for upload files to azure using ASP.Net,C#

June 24, 2011 5 comments

Simple way to Upload files to azure blob  

When we are dealing with windows azure application in ASP.Net we need to communicate with blobs in the azure from the asp.net code. Here we are going to demonstrate how we can upload files from local system to windows azure storage space using asp.net code. The application offers user to upload all files in the mapped local folder to a blob in the azure. It is a usefull tool for working with windows azure storage for upload files.

Requirements need to upload files in to azure blob

For upload files from local folder to azure we must need a azure storage account name and account key. We can purchase it from Microsfot. 

Steps to upload files into azure storage

  1. Create a simple windows application from visual studio, file >> new project >> visual C# >> Windows >> Windows Forms Application
  2. Need to add reference ‘Microsoft.WindowsAzure.StorageClient.dll’ that we can available after installing
  3. Drag a Button(For upload), a textbox (For display status) and a ListBox (List files with error or success message)to the form.
  4. On Button Click function we are going to upload file from local folder (here C:\\myfiles we can configure by accepting browse option).  

UI Design for the upload file to Azure Storage application

C# Source Code for Upload files to Azure Storage  

private void button1_Click(object sender, EventArgs e)
{
if (txtFolderName.Text == "")
{
MessageBox.Show("Please select a folder to upload files");
return;
}
if (MessageBox.Show("Do you want to Upload files to Blob?",
    "Confirm Upload", MessageBoxButtons.YesNo) == DialogResult.Yes)
{               
lblHeader.Visible = false;
txtStatusMessage.Text = "Upload Starting...";
lstBxFiles.Items.Clear();
UploadFileIntoBlop(txtFolderName.Text.Trim());
lblHeader.Visible = true;
}
}

public void UploadFileIntoBlop(string fileFullPath)
{
try
{
string dataCenterSettingKey =
        "DefaultEndpointsProtocol=" + "http" + ";AccountName="
        + "YourAccountNameHere" + ";AccountKey=" +
        "YourAccountKeyHere";
string filepath = "";
CloudStorageAccount storageAccount =
CloudStorageAccount.Parse(dataCenterSettingKey);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
string csvfileblob = "migrationfiles";
CloudBlobContainer blobContainer =
blobClient.GetContainerReference(csvfileblob);
bool isError = false;
foreach (string file in Directory.GetFiles(fileFullPath))
{
string[] filename = file.Split('\\');
string theFile = filename[filename.Length - 1];
try
{
DateTime startDate = DateTime.Now;
filepath = file.Trim();
FileStream fileContent = ReadFile(filepath);
var blob = blobContainer.GetBlobReference(theFile);
blob.UploadFromStream(fileContent);
TimeSpan difference = DateTime.Now.Subtract(startDate);
int totalSeconds = (int)Math.Ceiling(difference.TotalMinutes);
lstBxFiles.Items.Add(theFile +
" _Success (" + totalSeconds + " mnts)");
}
catch (Exception ex)
{
isError = true;
lstBxFiles.Items.Add(theFile + " _Error");
}
}
if (isError)
txtStatusMessage.Text = "Process completed with errors";
else
txtStatusMessage.Text = "Upload done successfully";
}
catch (Exception ex)
{
txtStatusMessage.Text = "Eror Ocurred : " + ex.Message;
}
}

public FileStream ReadFile(string fileFullPath)
{
FileStream fileStream = null;
if (System.IO.File.Exists(fileFullPath))
{
fileStream = new FileStream(fileFullPath,
FileMode.Open, FileAccess.Read);
}
return fileStream;
}

private void btnBrowse_Click(object sender, EventArgs e)
{
DialogResult result = this.folderBrowserDialog1.ShowDialog();
if (result == DialogResult.OK)
{
txtFolderName.Text = this.folderBrowserDialog1.SelectedPath;
}
}

 Run the application, then press upload button then in the button click event, we are calling UploadFileIntoBlop function. Then it will take all files under the local folder (folder path can be passed as parameter), and uploaded to a container (here it is migration files). In the middle of uploading to blob, we are adding all file names under the mentioned local folder in to list box with the status of whether it is uploaded or not. We can use this application as the tool for upload files from local to azure blob storage.

Advertisements
%d bloggers like this: