【问题标题】:Accessing Azure File Storage from Azure Function从 Azure Function 访问 Azure 文件存储
【发布时间】:2017-02-27 18:56:06
【问题描述】:

我正在尝试从 Azure 文件存储中检索文件以供在 Azure 函数中执行的 .exe 使用,并且似乎无法通过 UNC 凭据。

我的应用程序从 Azure SQL 数据库中获取 UNC 文件路径,然后尝试导航到该 UNC 路径(在 Azure 文件存储中)以导入文件的内容。我可以在 Windows 资源管理器中从我的 PC 导航到文件位置,但系统会提示我输入凭据。

我在执行应用程序之前尝试使用“net use”命令,但它似乎没有通过身份验证。

net use \\<storage account>.file.core.windows.net\<directory>\ /u:<username> <access key>

MyApp.exe 

Azure 函数日志错误:

Unhandled Exception: System.UnauthorizedAccessException: Access to the path '<file path>' is denied.

如果可能,我宁愿不修改我的 C# 应用并在 Azure 函数中进行身份验证(目前它是一个批处理函数,并且将基于计时器)。

【问题讨论】:

  • 阅读此链接表明您应该能够将NET USE 命令与您的凭据一起使用,或者您可以使用CMDKEY 命令存储您的凭据,然后使用NET USE 命令没有您的凭据。 azure.microsoft.com/en-us/documentation/articles/…
  • 好主意,但 Azure 函数似乎不喜欢 CMDKEY2016-10-18T15:40:22.425 CMDKEY: Credentials cannot be saved from this logon session.

标签: azure batch-file azure-sql-database azure-functions


【解决方案1】:

您不能使用 SMB (445/TCP)。函数在应用服务沙箱内运行。

来自https://github.com/projectkudu/kudu/wiki/Azure-Web-App-sandbox#restricted-outgoing-ports

限制输出端口

无论地址如何,应用程序都无法使用端口 445、137、138 和 139 连接到任何地方。换句话说,即使连接到非私有IP地址或虚拟网络地址,也不允许连接到端口445、137、138和139。

Use the Azure Storage SDK 与您的 Azure 文件端点对话:

using Microsoft.Azure;  // Namespace for CloudConfigurationManager
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.File;

// Parse the connection string and return a reference to the storage account.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
    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())
{
    // Get a reference to the root directory for the share.
    CloudFileDirectory rootDir = share.GetRootDirectoryReference();

    // Get a reference to the directory we created previously.
    CloudFileDirectory sampleDir = rootDir.GetDirectoryReference("CustomLogs");

    // Ensure that the directory exists.
    if (sampleDir.Exists())
    {
        // Get a reference to the file we created previously.
        CloudFile file = sampleDir.GetFileReference("Log1.txt");

        // Ensure that the file exists.
        if (file.Exists())
        {
            // Write the contents of the file to the console window.
            Console.WriteLine(file.DownloadTextAsync().Result);
        }
    }
}

示例使用CloudConfigurationManager - 我认为对于这样一个简单的场景来说这有点太多了。我会这样做:

using System.Configuration;

// "StorConnStr" is the Storage account Connection String
// defined for your Function in the Azure Portal
string connstr = ConfigurationManager.ConnectionStrings["StorConnStr"].ConnectionString;

【讨论】:

  • 感谢您的回复。类似于上面答案的评论。我正在尝试这种方法并且能够将内容打印到控制台窗口。不过,我在将其放入 DataTable 时遇到了麻烦。我正在使用ClosedXML.Excel.WorkbookClosedXML.Excel.WorkSheet,之前只是在UNC 路径中发送。现在这似乎不可能,因为我不断收到“用户名和密码不正确”。有没有办法将file.DownloadTextAsync().Result 之类的东西传递给ClosedXML.Excel.XLWorkbook
  • 读入UNC类型以外的数据的方法是否有覆盖?
  • 这在门户中的 Azure Functions 中是否有效?我收到错误:名称空间“Microsoft.WindowsAzure.Storage”中不存在类型或名称空间名称“文件”并且错误:名称空间“Microsoft.WindowsAzure.Storage”中不存在类型或名称空间名称“CloudStorageAccount” ' 在我看来,函数运行时 1 和 2(预览版)不允许更改目标框架,并且默认选择无法访问存储的东西。或者我只是错过了一些东西:)
  • 应该可以,但您需要该存储 nuget 的特定版本。查一下,应该匹配 Functions v1 使用的任何版本。如果可以的话,你真的应该使用 v2。
【解决方案2】:

我认为不可能在 Azure Function 中安装 Azure File Service Share,因为您无法访问底层基础架构 (same deal as WebApps)。

您可以做的是使用Azure Storage SDK,它是Azure Storage REST API 的包装器,并在您的应用程序中使用它与您的文件服务共享中的文件进行交互。

【讨论】:

  • 感谢您的快速回复。我希望不必修改我的应用程序即可从 Azure 函数访问 Azure 文件存储,但我会研究 Azure 存储 SDK。
  • 很遗憾,这是目前唯一可用的选项。
  • 我们确实有一个未解决的问题,要向 WebJobs SDK 和函数添加 Azure 文件支持(请参阅 here)。没有预计到达时间。
  • 使用此处的示例:azure.microsoft.com/en-us/documentation/articles/… 我仍然无法绕过“用户名或密码不正确”错误。如果我使用“net use”在本地安装驱动器,它工作正常。但是在 Azure Function 的上下文中,我无法安装驱动器。 if (file.Exists()) 返回 true,但 Closed.XML.Excel.XLWorkbook MyWorkbook = new XLWorkbook(&lt;UNC path&gt;); 会产生用户名/密码不正确的错误。
猜你喜欢
  • 2020-10-10
  • 2018-09-26
  • 2021-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-25
相关资源
最近更新 更多