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;
}
}