簡體   English   中英

Azure function 連接到本地 sql 訪問被拒絕

[英]Azure function connect to on-premises sql access denied

I've got an issue where I cannot connect to an on-premises SQL database through an Azure function app once it is published to Azure. 但是,當我在 Visual Studio Code 中本地運行它時,它運行良好。

Function 的目的是將存儲在 Azure blob 存儲中的圖像大小調整為縮略圖並將其存儲在表字段 <varbinary(max)> 中。

這是我收到的錯誤消息:

2020-12-15T15:33:52.058 [Information] A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

2020-12-15T15:33:52.093 [Error] Executed 'BlobTrigger_MQS_Resize' (Failed, Id=346672c6-820c-43f6-b950-d5059e44697e, Duration=19570ms)
Access is denied.

我正在通過 SQL 登錄連接到 SQL 數據庫,效果很好。 連接字符串在 local.settings.json 中:

{
  "IsEncrypted": false,
  "Values": {
    ...,
    "Connection_SQL_Database_MQS": "Data Source=MyServer;Initial Catalog=MyDB;User ID=MyUser;Password=MyPassword;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"
  }
}

我對問題的核心進行了一些簡化的代碼(在 C# 中):

public static class BlobTrigger_MQS_Resize
    {
        [FunctionName("BlobTrigger_MQS_Resize")]
        public static async Task Run([BlobTrigger("mqs-media/{name}", Connection = "hqdevstorage_STORAGE")]Stream input, string name, ILogger log)
        {
            log.LogInformation($"C# Blob trigger function Processed blob\n Name:{name} \n Size: {input.Length} Bytes");

            try
            {
                var connectionString = Environment.GetEnvironmentVariable("Connection_SQL_Database_MQS", EnvironmentVariableTarget.Process);

                using (var output = new MemoryStream())
                using (Image<Rgba32> image = Image.Load(input))
                using (SqlConnection conn = new SqlConnection(connectionString))
                {
                    // Code that resizes images to 'output', this works also when published
                    image.Resize(...);           
                    image.Save(output, encoder);

                    //PROBLEM IS HERE: Write resized image to SQL Database
                    conn.Open();
                    var query = "UPDATE dbo.MyTable" 
                    + " SET Blob = @output"  
                    + " WHERE File_Name = '" + name + "'";

                    using(SqlCommand cmd = new SqlCommand(query, conn))
                    {
                        cmd.Parameters.AddWithValue("@output", output);
                        var rows = await cmd.ExecuteNonQueryAsync();
                        log.LogInformation($"{rows} rows were updated");
                    }
                }
                
            }
            catch (Exception ex)
            {
                log.LogInformation(ex.Message);
                throw;
            }
             
        }
    }

有人可以幫我弄這個嗎? 我不知道為什么它在本地工作但不在 Azure 上工作。

服務器未找到或無法訪問。

這是網絡連接問題,而不是權限問題。 The Azure function cannot resolve your SQL Server's hostname into an IP address, and would not be able to connect to that IP address if it could.

您的 function 需要訪問本地連接的 VNet 或使用混合連接來訪問您的本地 SQL 服務器。

https://docs.microsoft.com/en-us/azure/app-service/app-service-hybrid-connections

https://docs.microsoft.com/en-us/azure/azure-functions/functions-create-vnet

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM