如何在Azure中运行包含包的Python脚本并将其连接到Azure SQL数据库?

agyaoht7  于 10个月前  发布在  Python
关注(0)|答案(1)|浏览(37)

我有一个Azure SQL Server安装程序,其中包含来自各种来源的数据。这些数据可以通过我可以在python中连接的各种API获取。然后,我可以在python中连接到Azure SQL服务器并输入数据。这一切都在本地运行完美,但我在将此系统放入Azure时遇到了麻烦。脚本应该在特定的时间(至少一天一次,可能更多)和需要手动刷新时运行。要做到这一点,其中一个选项是使用脚本创建一个Azure函数,并使用http触发器从Power Automate调用它。
问题似乎是Azure SQL服务器的防火墙无法以某种方式配置,因此Azure功能可以在没有高级计划的情况下访问,这对于每天运行几分钟的程序来说是多余的。最重要的是,由于对包的依赖性,该函数必须在本地开发,然后发布到Azure。这意味着,如果以后必须更改函数,则必须创建一个新的本地函数,复制代码,然后修改并再次发布到Azure,这对我来说似乎效率低下。
那么,有没有一种方法可以配置Azure函数来访问Azure SQL服务器,或者有其他方法可以做到这一点?
编辑:这里是连接到SQL Server的代码。它在本地工作,但不在Azure函数中工作。

#Set the environment of the database through the url
authority_url='url'
context = adal.AuthenticationContext(authority_url, api_version=None)

applicationID='ID'
secret='secret'
token = context.acquire_token_with_client_credentials("https://database.windows.net/", applicationID, secret)

#Create connection string
driver='{ODBC Driver 18 for SQL Server}'
server='server'
database='db'
connString = f"Driver={driver};SERVER={server};DATABASE={database}"

#Convert the token to a byte string
tokenb = bytes(token['accessToken'], 'UTF-8')
exptoken = b''
for i in tokenb:
    exptoken += bytes({i})
    exptoken += bytes(1)
    tokenstruct = struct.pack('=i', len(exptoken)) + exptoken
    
#Connect to the database
SQL_COPT_SS_ACCESS_TOKEN = 1256
dbEngine = pyodbc.connect(connString, attrs_before={SQL_COPT_SS_ACCESS_TOKEN: tokenstruct})

字符串

e5nszbig

e5nszbig1#

通过使用此reference在Azure SQL Server的网络选项卡中添加IP地址。


的数据

driver='{ODBC Driver 18 for SQL Server}'
server='server'
database='db'
connString = f"Driver={driver};SERVER={server};DATABASE={database}"

#Convert the token to a byte string
tokenb = bytes(token['accessToken'], 'UTF-8')
exptoken = b''
for i in tokenb:
    exptoken += bytes({i})
    exptoken += bytes(1)
    tokenstruct = struct.pack('=i', len(exptoken)) + exptoken
    
#Connect to the database
SQL_COPT_SS_ACCESS_TOKEN = 1256
dbEngine = pyodbc.connect(connString, attrs_before={SQL_COPT_SS_ACCESS_TOKEN: tokenstruct})

字符串
单击允许资源。


  • 通过在Azure SQL Server的网络选项卡中添加IP地址



  • 有关详细信息,请参阅Azure SQL DatabasesSO

相关问题