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

标签 c# asp.net .net azure azure-storage-files

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

我可以进入我的服务器并使用“net use”命令将文件共享安装为驱动器。因为对previous question的回答,我知道 ASP.NET 将无法看到已安装的驱动器。

我能够在本地计算机上运行此功能,在本地用户下运行 IIS 并将 azure 存储帐户文件共享添加到凭据管理器,但这在服务器上不起作用,因此我缺少一些拼图的一部分。

在服务器上,如果我尝试使用访问共享

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

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

我尝试将凭据添加到凭据管理器,并在运行凭据管理器的用户下运行我的应用程序池,但我仍然从 Directory.Exists() 返回“false”。

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

最佳答案

建议您使用Storage Client Library来访问文件共享,查看官方文章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    
    }

关于c# - 从 ASP.NET 应用程序访问 Azure 存储帐户文件共享,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35759421/

相关文章:

c# - 无法在 Entity Framework 中的 DbCommand 中传递参数

c# - Web API - 如何在 Controller DateTime ('dd/MM/yyyy' 中接收作为 Url 参数?

asp.net - RegisterClientScriptBlock困惑

c# - Socket.ConnectAsync收到400错误请求,而Socket.Connect获得200正常

.net - Microsoft .NET 3.5 完整下载

c# - 为什么我无法访问属于不同类型成员的事件的成员?

c# - 将非实例化的类和接口(interface)传递给泛型方法

c# - 如何用C#一次性更新ORACLE中的多行(一次性IO)否则也不是不可能发生

c# - CodeBehind 中的 GridView 选择行和回发

.net - 如何以编程方式启动 visual studio 并将其发送到特定文件/行?