【问题标题】:Can't read/write to Azure Files share from my .Net code无法从我的 .Net 代码读取/写入 Azure 文件共享
【发布时间】:2017-10-12 00:23:28
【问题描述】:

我编写了一个在 Azure Windows Server 2016 VM 中运行的服务。当它从队列中获取作业时,它会生成另一个程序,该程序生成 PDF 并将其保存到磁盘。我正在读取配置文件并使用 streamreader/streamwriter 将 HTML 文件(使用第三方组件转换为 PDF)保存到磁盘。

但是对于所有磁盘访问 Azure 文件 SMB 共享,我不断收到 path could not be found 错误版本。如果我使用本地磁盘,它工作正常。

我已经仔细检查了它是否使用了正确的路径并且该路径确实存在(简单地说:)。

多年来,它在托管应用服务器上运行良好。我现在正在尝试将所有内容移至 Azure。

任何想法我错过了什么或做错了什么?

编辑:

看来我遇到了这个问题:https://serverfault.com/questions/177139/windows-service-cant-access-network-share

但我无法在此处执行相同的解决方案,因为使用 Azure 文件没有可以添加用户的远程服务器。

一位 MSDN 论坛用户建议使用 Azure Storage Client Library。我的第三方 PDF 组件无法重新编程以使用 Azure 客户端存储库,因此我只能在本地驱动器上完成所有工作,然后将最终的 PDF 文件复制到 Azure 文件中。

这将是一个完全可以接受的解决方案。但我不知道如何把它拉下来。

【问题讨论】:

  • 请编辑您的问题以显示一些详细信息(例如您的代码、实际共享配置等)。没有看到任何细节,你只会得到猜测。
  • “在 Windows 服务器上运行的服务”到底是什么意思?是 Windows 服务还是公开 Web 服务的 Web 应用程序?还有一点:我理解 P: 是映射网络共享。您在哪个用户下创建了映射?运行服务的用户是否相同?还添加有关您遇到的错误的信息/完整堆栈跟踪

标签: vb.net azure azure-storage mapped-drive


【解决方案1】:

您无法从服务或从服务生成的程序对 Azure Files SMB 共享进行“正常”文件访问。您可以以编程方式将文件复制到存储帐户和从存储帐户复制到本地驱动器。您必须使用本地驱动器来创建和编辑文件,然后将它们复制回您的存储帐户。

这是一些 VB.Net 代码,用于将本地文件复制到 Azure 文件共享(有效但未清理):

Imports Microsoft.WindowsAzure.Storage
Imports Microsoft.WindowsAzure.Storage.File
Imports System.Configuration

    Dim StorageAccount As CloudStorageAccount
    Dim file As FileInfo
    Dim fileClient As CloudFileClient
    Dim share As CloudFileShare
    Dim root As CloudFileDirectory
    Dim dir As CloudFileDirectory
    Dim cloudFile As CloudFile

    Try
        file = New FileInfo(InFileName) ' includes full path to file
        StorageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings.Get("AzureFilesConnString"))

        fileClient = StorageAccount.CreateCloudFileClient()
        share = fileClient.GetShareReference("sharename")
        root = share.GetRootDirectoryReference()
        dir = root.GetDirectoryReference("PDFs") ' Note that you apparently can't copy to the root (\) folder 

        cloudFile = dir.GetFileReference(OutFileName) ' Only the file name, not full URI

        Using fs As FileStream = file.OpenRead()
            cloudFile.UploadFromStream(fs)
        End Using
    Catch ex As StorageException
        Debug.Print(ex.Message)
        Debug.Print(ex.RequestInformation.Exception.ToString)
    Catch ex As Exception
        Debug.Print(ex.Message)
    End Try

【讨论】:

  • “您不能从服务或从服务衍生的程序对 Azure 文件 SMB 共享进行“正常”文件访问。”这在任何地方都有记录吗?
猜你喜欢
  • 1970-01-01
  • 2017-03-21
  • 1970-01-01
  • 2013-02-21
  • 2020-11-06
  • 1970-01-01
  • 2021-01-10
  • 1970-01-01
  • 2016-05-29
相关资源
最近更新 更多