upload files to Azure file storage from web app using rest api
Solution 1
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/
Solution 2
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();
}
}
AprilX
Updated on January 19, 2021Comments
-
AprilX over 3 years
I have a web app that is currently using webforms, not MVC, which is going to be hosted on the Azure platform.
The main function of this web app is to upload user files to Azure File Storage.
The files may be pdf, mp3, etc., not simple text or data stream or data input.
I am told to use Azure REST API to upload files, but I am really not familiar with it and can't find a good sample or tutorial or video online. The current documents from Microsoft reads like ?????? to me.
Currently I just upload to a local folder, so the code looks like:
FileUpload1.PostedFile.SaveAs(Server.MapPath("fileupload\\" + FileUpload1.FileName));
in C#;Where do I go from there? I think I am supposed to add a StorageConnectionString which looks like
DefaultEndpointsProtocol=https;AccountName=xxx;AccountKey=yyy
, which I already have.And then I should write some code like 'post' in C#?
-
AprilX almost 8 yearsoh my, I am crying, this is so helpful, thank you! Save my day. solved most of the problem!
-
AprilX almost 8 yearsJust that why we use "await"? Is it necessary? When I use it, error says "await operator can only be used within an Async method", when I add async to my "protected void Button1_Click" (which is the button click for upload file), changing it to "protected async void", another error will show as "An asynchronous operation cannot be started at this time"...... I feel ashamed that I don't understand so many things (T . T)
-
Gary Holland almost 8 yearsthe
UploadFromStreamAsync
is an asynchronous method. So you either need to put it in a method with asyncstatic async void UploadFile()
, or use the Wait() function to make it synchronous.cloudFile.UploadFromStreamAsync(fileStream).Wait();
. good luck. -
AprilX almost 8 yearsthank you, after some work I finally solved this problem and I will attach my final code below, thank you for your help! I hope this question will help those that have a similar problem.
-
RJN about 5 years@GaryHolland Is there any other way we can directly upload the file into cloud directory instead of creating reference of the file in directory and do asynch copy?