从 ASP.NET 应用程序访问 Azure 存储帐户文件共享

本文关键字:存储 文件共享 Azure 访问 ASP NET 应用程序 | 更新日期: 2023-09-27 18:37:10

我有一个需要从 Azure 文件共享读取和写入的 ASP.NET 应用程序。我无法直接在 Azure 文件共享上设置权限,而是拥有存储帐户密钥和主访问密钥。

我可以进入我的服务器并使用"net use"命令将文件共享挂载为驱动器。由于上一个问题的答案,我知道 ASP.NET 将无法看到装载的驱动器。

我能够从我的本地计算机使其工作,在本地用户下运行 IIS,并将 azure 存储帐户文件共享添加到凭据管理器,但这在服务器上不起作用,所以我错过了一些难题。

在服务器上,如果我尝试使用

Directory.Exists(@"''storageaccountkey.file.core.windows.net'sharename");

这在 ASP.NET 不起作用(我怀疑这是因为 ASP.NET 无法对共享进行身份验证)。从 LINQpad 运行此代码确实有效并返回 true。

我尝试将凭据添加到凭据管理器,

并在运行凭据管理器的用户下运行我的应用程序池,但我仍然从 Directory.Exists() 返回"false"。

为什么我看不到服务器上的目录?

从 ASP.NET 应用程序访问 Azure 存储帐户文件共享

我建议您使用存储客户端库访问文件共享,请查看官方文章 https://azure.microsoft.com/en-us/documentation/articles/storage-dotnet-how-to-use-files/

您可以使用下面的代码而不是 Directory.Exists 来访问共享:

    // Parse the connection string for the storage account.
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(Microsoft.Azure.CloudConfigurationManager.GetSetting("StorageConnectionString"));
    // Create a CloudFileClient object for credentialed access to File storage.
    CloudFileClient fileClient = storageAccount.CreateCloudFileClient();
    // Get a reference to the file share we created previously.
    CloudFileShare share = fileClient.GetShareReference("logs");
    // Ensure that the share exists.
    if (share.Exists())
    {
        //do something    
    }