将文件从Asp.Net写入Azure文件存储失败

时间:2016-02-01 08:59:18

标签: asp.net azure azure-virtual-machine smb azure-storage-files

我有一个带有IIS设置的Azure VM,它托管我的ASP.Net网站。

我希望能够使用普通的Windows类库(System.IO)从网络共享(Azure文件存储)读取和写入文档/文件。我不想使用Azure存储客户端库,也不想使用Azure Storage Rest API。

我已按照this article中的详细说明安装了共享并保留了我的凭据。

安装驱动器并保留我的凭据后,我指向我的网站以使用我的共享驱动器来处理所有与文档管理相关的功能。下载文件非常有效。但是,只要我尝试创建文件夹或将文档写入网络共享,我就会收到错误。

我收到的错误是:

  

找不到路径的一部分&f; [RestOfThePathHere]'。   堆栈跟踪:

     

[DirectoryNotFoundException:找不到路径的一部分   ' F:\测试\ MyThing \客户\ TheirStuff&#39 ;.]
  System.IO .__ Error.WinIOError(Int32 errorCode,String maybeFullPath)   +338 System.IO.Directory.InternalCreateDirectory(String fullPath,String path,Object dirSecurityObj,Boolean checkHost)+1578
  System.IO.Directory.InternalCreateDirectoryHelper(String path,Boolean   checkHost)+213
  MyStuff.FileHandling.FileExtenders.SaveToFile(Byte [] ByteArray,String   目录,字符串FileName)+116
  MyStuff.TheirStuff.BLL.TheirStuffTestClient.RunEnquiry(ClientBase   client,String resultType)+3776
  MyStuff.MyThing.UI.Views.ClientController.CreditCheck(Int64 id)+42
  lambda_method(Closure,ControllerBase,Object [])+114
  System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext   controllerContext,IDictionary 2 parameters) +242
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary
2   参数)+39
  System.Web.Mvc.Async.AsyncControllerActionInvoker.b__39(IAsyncResult的   asyncResult,ActionInvocation innerInvokeState)+12
  System.Web.Mvc.Async.WrappedAsyncResult 2.CallEndDelegate(IAsyncResult asyncResult) +139
System.Web.Mvc.Async.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d() +112 System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +452 System.Web.Mvc.Async.<>c__DisplayClass33.<BeginInvokeActionMethodWithFilters>b__32(IAsyncResult asyncResult) +15
System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +37 System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +241
System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +29
System.Web.Mvc.Async.WrappedAsyncVoid
1.CallEndDelegate(IAsyncResult的   asyncResult)+111
  System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult)+53   System.Web.Mvc.Async.WrappedAsyncVoid 1.CallEndDelegate(IAsyncResult asyncResult) +19
System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +51
System.Web.Mvc.Async.WrappedAsyncVoid
1.CallEndDelegate(IAsyncResult的   asyncResult)+111
  System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()   +606 System.Web.HttpApplication.ExecuteStep(IExecutionStep step,Boolean&amp; completedSynchronously)+288

OR,当我使用UNC寻址时

  

找不到网络路径。   StackTrace:[IOException:找不到网络路径。 ]
  System.IO .__ Error.WinIOError(Int32 errorCode,String maybeFullPath)   +14382132 System.IO.Directory.InternalCreateDirectory(String fullPath,String path,Object dirSecurityObj,Boolean checkHost)+1578   System.IO.Directory.InternalCreateDirectoryHelper(String path,Boolean   checkHost)+213
  MyStuff.FileHandling.FileExtenders.SaveToFile(Byte [] ByteArray,String   目录,字符串FileName)+116
  MyStuff.TheirStuff.BLL.TheirStuffTestClient.RunEnquiry(ClientBase   client,String resultType)+3776
  MyStuff.MyThing.UI.Views.ClientController.CreditCheck(Int64 id)+42
  lambda_method(Closure,ControllerBase,Object [])+114
  System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext   controllerContext,IDictionary 2 parameters) +242
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary
2   参数)+39
  System.Web.Mvc.Async.AsyncControllerActionInvoker.b__39(IAsyncResult的   asyncResult,ActionInvocation innerInvokeState)+12
  System.Web.Mvc.Async.WrappedAsyncResult 2.CallEndDelegate(IAsyncResult asyncResult) +139
System.Web.Mvc.Async.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d() +112 System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +452 System.Web.Mvc.Async.<>c__DisplayClass33.<BeginInvokeActionMethodWithFilters>b__32(IAsyncResult asyncResult) +15
System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +37 System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +241
System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +29
System.Web.Mvc.Async.WrappedAsyncVoid
1.CallEndDelegate(IAsyncResult的   asyncResult)+111
  System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult)+53   System.Web.Mvc.Async.WrappedAsyncVoid 1.CallEndDelegate(IAsyncResult asyncResult) +19
System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +51
System.Web.Mvc.Async.WrappedAsyncVoid
1.CallEndDelegate(IAsyncResult的   asyncResult)+111
  System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()   +606 System.Web.HttpApplication.ExecuteStep(IExecutionStep step,Boolean&amp; completedSynchronously)+288

更新 - 我当前的代码

MVC控制器方法

public ActionResult Upload(DocumentUploadVM VM, long ClientId, long ContextInstanceId, long DocumentClassId, long TaskId)
    {
            var documentClass = docMan.Get_DocumentClass(DocumentClassId);

            var errors = new List<string>();

            if (VM.File == null || VM.File.ContentLength == 0)
                errors.Add("You haven't selected a file to upload");
            else if (!documentClass.MimeTypeAllowed(VM.File.ContentType, docMan))
                errors.Add("The file type is forbidden");

            if (errors.Count == 0 && ModelState.IsValid)
            {
                var path = settings.GetSettingValueAsString("Document Management: Client Documents Root Path").TrimEnd('\\') + "\\Uploads\\";
                var pvtFileName = new Random().Next().ToString() + "." + VM.File.FileName.Split('.').Last();
                var targetURI = Path.Combine(path, pvtFileName);

                MemoryStream ms = new MemoryStream();
                VM.File.InputStream.CopyTo(ms);

                IFileHandler fh = FileHandlingFactory.Create();
                fh.SaveFile(ms, targetURI);

                docMan.Add_Document(
                    new FileItem
                    {
                        FilePathOnServer = path,
                        PublicFileName = VM.File.FileName,
                        PrivateFileName = pvtFileName,
                        MimeType = VM.File.ContentType
                    },
                    docMan.Get_ContextInstance(ContextInstanceId),
                    new List<DocumentClass> { documentClass });
            }
            else
            {
                TempData["errors"] = errors;
            }

            //TODO: In case of model errors, this call will suppress the errors.  Look to TempData / PRG pattern to maintain model state over redirect.
            return
              RedirectToAction("ListClientDocuments", new { ClientId = ClientId, TaskId = TaskId });
        }

IFileHandler.SaveFile实现

public override void SaveFile(MemoryStream MemoryStream, string Fullpath)
    {
        var directory = Fullpath.Substring(0, Fullpath.LastIndexOf('\\')).TrimEnd('\\');
        var fileName = Fullpath.Replace(directory, "").Trim('\\');
        MemoryStream.ToArray()
        .SaveToFile(directory, fileName);
    }
PS - 请原谅可怕的代码和公然无视C#编码标准。我写这篇文章时,我是个菜鸟。但是,当用于解决本地文件存储时,此代码尚未给我任何问题。

1 个答案:

答案 0 :(得分:1)

我认为您可能需要尝试创建映射到存储帐户用户的本地用户,然后将此用户添加到IIS_IUSRS组。

有关详细信息,请参阅此博客 http://blogs.iis.net/davidso/azurefile

-Jason