question

DanielBernard-9068 avatar image
0 Votes"
DanielBernard-9068 asked ·

Web App Can't Access File Store

I've created a .NET 4.72 MVC web app. One of the functions is to upload a file to a file store. I have the code written, when I run it in debug mode, it works and the file is stored on the Azure file store / file share. However, when I deploy the app to Azure App service, the code throws an error, and can't access the store. The IP address of our local network that I deployed the app from is allowed in the Azure virtual network I created that is attached to the file store. That's why it works in debug I think. The storage account details are a web.config item in my app. I pasted the C# code below. Any help would be appreciated. A 403 Forbidden error is thrown on the if (share.exists()) line.

       private string StoreFile(HttpPostedFileBase _postedFile)
         {
             string documentsConnectionString = ConfigurationManager.AppSettings["AzureStorageAccount"];
             string fileShareReference = ConfigurationManager.AppSettings["AzureFileShare"];
             string rootDirectory = ConfigurationManager.AppSettings["AzureFileRoot"];
             string subDirectory = ConfigurationManager.AppSettings["AzureAttachments"];
 
 
             CloudStorageAccount storageAccount;
             storageAccount = CloudStorageAccount.Parse(documentsConnectionString);
             CloudFileClient fileClient = storageAccount.CreateCloudFileClient();
             CloudFileShare share = fileClient.GetShareReference(fileShareReference);
             if (share.Exists())
             {
                 try
                 {
                     CloudFileDirectory rootDir = share.GetRootDirectoryReference();
 
                     CloudFileDirectory directoryRef;
 
                     directoryRef = rootDir.GetDirectoryReference(rootDirectory);
 
                     directoryRef.CreateIfNotExists();
 
                     CloudFileDirectory subDirectoryRef;
                     subDirectoryRef = directoryRef.GetDirectoryReference(subDirectory);
 
                     subDirectoryRef.CreateIfNotExists();
 
                     string extension = Path.GetExtension(_postedFile.FileName);
                     string[] fileSplit = _postedFile.FileName.Split('.');
                     string uploadFileName = fileSplit[0];
                     string fileNameSave = uploadFileName + '_' + DateTime.Now.ToString("MMddyyyy_Hmmssfff") + extension;
 
                     //Read the uploaded File as Byte Array from FileUpload control.
                     Stream fs = _postedFile.InputStream;
                     BinaryReader br = new BinaryReader(fs);
                     byte[] fileInBytes = br.ReadBytes((Int32)fs.Length);
 
                     CloudFile file = subDirectoryRef.GetFileReference(fileNameSave);
 
                     file.UploadFromByteArray(fileInBytes, 0, fileInBytes.Count());
 
                     return fileNameSave;
                 }
                 catch (Exception ex)
                 {
                     //Logger.Log("Uploading file to azure exception: " + ex.Message);
                     return string.Empty;
                 }
             }
             else
             {
                 //Logger.Log("Document share does not exist!");
                 return string.Empty;
             }
 
         }
 
azure-webapps
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

ryanchill avatar image
0 Votes"
ryanchill answered ·

Hi @DanielBernard-9068,

Make sure your app is in the same VNet as your storage account and add Microsoft.Storage as a service endpoint. This will allow your app to access your storage since it seems your storage account is setup for private access. Another option is to change the access from private to public and use shared keys instead.

Hope this helps.

· Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.