Web App Can't Access File Store

Daniel Bernard 1 Reputation point
2020-04-13T20:36:34.93+00:00

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 App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
6,858 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Ryan Hill 25,486 Reputation points Microsoft Employee
    2020-04-14T22:09:09.967+00:00

    Hi @Daniel Bernard ,

    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.

    0 comments No comments