upload files to Azure file storage from web app using rest api
This piece of code is based on the answer I get from Gary Holland above. I hope other people benefit from it. I am not good at programming, hopefully the code looks ok.
if (FileUpload1.HasFile)
{
//Connect to Azure
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["StorageConnectionString"].ConnectionString);
// Create a reference to the file client.
CloudFileClient fileClient = storageAccount.CreateCloudFileClient();
// Get a reference to the file share we created previously.
CloudFileShare share = fileClient.GetShareReference("yourfilesharename");
if (share.Exists())
{
// Generate a SAS for a file in the share
CloudFileDirectory rootDir = share.GetRootDirectoryReference();
CloudFileDirectory sampleDir = rootDir.GetDirectoryReference("folderthatyouuploadto");
CloudFile file = sampleDir.GetFileReference(FileUpload1.FileName);
Stream fileStream = FileUpload1.PostedFile.InputStream;
file.UploadFromStream(fileStream);
fileStream.Dispose();
}
}
Azure provide a nuget library that you can use to upload, and do other "file management" types of activities on Azure File Storage.
The library is called:
WindowsAzure.Storage
UPDATE: The new library to use is Azure.Storage.Blobs.
Here are the basics of getting this going:
//Connect to Azure
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
// Create a reference to the file client.
CloudFileClient fileClient = storageAccount.CreateCloudFileClient();
// Create a reference to the Azure path
CloudFileDirectory cloudFileDirectory = GetCloudFileShare().GetRootDirectoryReference().GetDirectoryReference(path);
//Create a reference to the filename that you will be uploading
CloudFile cloudFile = cloudSubDirectory.GetFileReference(fileName);
//Open a stream from a local file.
Stream fileStream= File.OpenRead(localfile);
//Upload the file to Azure.
await cloudFile.UploadFromStreamAsync(fileStream);
fileStream.Dispose();
More links and info here (note scroll a fair way down for samples): https://azure.microsoft.com/en-us/documentation/articles/storage-dotnet-how-to-use-files/