在Azure函数中将连接字符串设置为App设置/环境变量 [英] Setting Connection String as App Setting/Environment Variable in Azure Function

查看:157
本文介绍了在Azure函数中将连接字符串设置为App设置/环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Azure函数中,我为数据库连接字符串指定了环境变量/应用程序设置.当我使用VS Code和Python在Azure数据科学虚拟机上本地运行函数时,可以使用环境变量.

In my Azure Function, I have specified an Environment Variable/App Setting for a database connection string. I can use the Environment Variable when I run the Function locally on my Azure Data Science Virtual Machine using VS Code and Python.

但是,当我将函数部署到Azure时,出现错误:KeyValue为None,这意味着它找不到连接字符串的环境变量.看到错误:

However, when I deploy the Function to Azure, I get an error: KeyValue is None, meaning that it cannot find the Environment Variable for the connection string. See error:

Exception while executing function: Functions.matchmodel Result: Failure
Exception: KeyError: 'CONNECTIONSTRINGS:PDMPDBCONNECTIONSTRING'
Stack:   File "/azure-functions 
  host/workers/python/3.7/LINUX/X64/azure_functions_worker/dispatcher.py", line 315, in 
  _handle__invocation_request self.__run_sync_func, invocation_id, fi.func, args)
File "/usr/local/lib/python3.7/concurrent/futures/thread.py", line 57, in run
  result = self.fn(*self.args, **self.kwargs)
File "/azure-functions-host/workers/python/3.7/LINUX/X64/azure_functions_worker/dispatcher.py", 
  line 434, in __run_sync_func
  return func(**params)
File "/home/site/wwwroot/matchmodel/__init__.py", line 116, in main
File "/home/site/wwwroot/matchmodel/production/dataload.py", line 28, in query_dev_database
  setting = os.environ["CONNECTIONSTRINGS:PDMPDBCONNECTIONSTRING"]
File "/usr/local/lib/python3.7/os.py", line 679, in __getitem__
  raise KeyError(key) from None'

我尝试了以下解决方案:

I have tried the following solutions:

  • 添加了"CONNECTIONSTRINGS"以在Python脚本中指定环境变量(使其在本地工作) setting = os.environ["CONNECTIONSTRINGS:PDMPDBCONNECTIONSTRING"]

  • Added "CONNECTIONSTRINGS" to specify the Environment Variable in the Python script (which made it work locally) setting = os.environ["CONNECTIONSTRINGS:PDMPDBCONNECTIONSTRING"]

使用logging.info(os.environ)在控制台中输出我的环境变量.我的连接字符串已列出.

Used logging.info(os.environ) to output my Environment Variables in the console. My connection string is listed.

在Azure Function门户中将连接字符串添加为应用程序设置".

Added the connection string as Application Setting in the Azure Function portal.

  • 在Azure Function门户中将连接字符串添加为连接字符串.

有人可以尝试其他解决方案吗?

Does anyone have any other solutions that I can try?

推荐答案

实际上,您几乎可以获得正确的连接字符串,但是使用了错误的前置字符串.您可以参考此文档的更多详细信息:

Actually you are almost get the right the connection string, however you use the wrong prepended string. Further more detailed information you could refer to this doc:Configure connection strings.

使用哪个字符串取决于您选择的类型,例如在我的测试中,我使用自定义类型.然后我应该使用os.environ['CUSTOMCONNSTR_testconnectionstring']来获取值.

Which string to use it depends on which type you choose, like in my test I use a custom type. Then I should use os.environ['CUSTOMCONNSTR_testconnectionstring'] to get the value.

从文档中您可以找到以下类型:

From the doc you could find there are following types:

  • SQL Server:SQLCONNSTR _
  • MySQL:MYSQLCONNSTR _
  • SQL数据库:SQLAZURECONNSTR _
  • 自定义:CUSTOMCONNSTR _

这篇关于在Azure函数中将连接字符串设置为App设置/环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆