我有一个存储帐户,其容器名为 abc360。该容器中有一个名为 sqlite_db_file 的文件夹。 Sqlite db 文件将被放置在此文件夹中。文件名如下:
ABC100102_2_20230202.db
文件路径如下所示:
abc360/sqlite_db_file/ABC100102_2_20230202.db
存储帐户和容器详细信息:
Storage account: abc360stg
Container: abc360
我在 python 中有一个 azure 函数应用程序,应该在删除文件并将数据从文件复制到 csv 文件并将其上传回同一路径时触发。
所以我创建了一个带有 blob 触发器的函数,function.json 如下所示:
{
"scriptFile": "__init__.py",
"bindings": [
{
"name": "myblob",
"type": "blobTrigger",
"direction": "in",
"path": "abc360/sqlite_db_file/{name}.db",
"connection": "abc360stg_STORAGE"
}
]
}
现在我只是想获取数据库文件中的所有表,下面是我的代码:
import logging
import sqlite3
import os
import csv
from azure.functions import InputStream
class DataMigrator:
def __init__(self, fileName, connection_string):
self.fileName = fileName
self.connection_string = connection_string
def connect_sqlite(self):
return sqlite3.connect(self.fileName)
def get_table_names(self, cursor_sqlite):
logging.info(cursor_sqlite)
cursor_sqlite.execute("SELECT name FROM sqlite_master;")
return cursor_sqlite.fetchall()
def main(myblob: InputStream):
try:
blob_name = myblob.name.split("/")[-1]
logging.info(blob_name)
migrator = DataMigrator(blob_name, connection_string)
conn_sqlite = migrator.connect_sqlite()
logging.info("Connected to SQLite database successfully")
cursor_sqlite = conn_sqlite.cursor()
tables = migrator.get_table_names(cursor_sqlite)
logging.info(f"Tables in SQLite file: {tables}")
except Exception as e:
logging.error(f"Error: {str(e)}")
finally:
# Close SQLite connection
if conn_sqlite:
conn_sqlite.close()
代码工作正常,它可以很好地连接到 sqlite 数据库文件,但它返回一个表列表的 emprt 数组。当我在本地连接本地驱动器中的文件夹时,它工作正常并列出所有内容。
输出(当数据库文件位于存储帐户中时):
Tables in SQLite file: []
使用以下代码是因为 myblob.name 返回 abc360/sqlite_db_file/ABC100102_2_20230303.db 而我想要的只是文件名:
myblob.name.split("/")[-1]
我想知道这里是否还需要其他东西来读取存储帐户中的数据库文件?
非常感谢您的帮助。
我尝试使用下面的代码从 Azure 存储 blob 中的 .db 文件获取表列表。
代码:
import logging
import os
import tempfile
import sqlite3
from azure.functions import InputStream
class DataMigrator:
def __init__(self, file_path):
self.file_path = file_path
def connect_sqlite(self):
return sqlite3.connect(self.file_path)
def get_table_names(self, cursor_sqlite):
cursor_sqlite.execute("SELECT name FROM sqlite_master WHERE type='table';")
return cursor_sqlite.fetchall()
def main(myblob: InputStream):
try:
blob_name = os.path.basename(myblob.name)
logging.info(f"Processing blob: {blob_name}")
temp_file_path = os.path.join(tempfile.gettempdir(), blob_name)
with open(temp_file_path, "wb") as temp_file:
temp_file.write(myblob.read())
migrator = DataMigrator(temp_file_path)
conn_sqlite = migrator.connect_sqlite()
logging.info("Connected to SQLite database successfully")
cursor_sqlite = conn_sqlite.cursor()
tables = migrator.get_table_names(cursor_sqlite)
logging.info(f"Tables in SQLite file: {tables}")
except Exception as e:
logging.error(f"Error: {str(e)}")
finally:
if conn_sqlite:
conn_sqlite.close()
if os.path.exists(temp_file_path):
os.remove(temp_file_path)
local.settings.json:
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "<storage_conne>",
"FUNCTIONS_WORKER_RUNTIME": "python",
"kam11funcstr_STORAGE": "<storage_conne>"
}
}
function.json:
{
"scriptFile": "__init__.py",
"bindings": [
{
"name": "myblob",
"type": "blobTrigger",
"direction": "in",
"path": "abc360/sqlite_db_file/{name}.db",
"connection": "kam11funcstr_STORAGE"
}
]
}
输出:
以下 blob 触发器函数代码成功运行,提供容器中 .db 文件中的表列表。
* Executing task: .venv\Scripts\activate ; func host start
Found Python version 3.11.8 (py).
Azure Functions Core Tools
Core Tools Version: 4.0.5382 Commit hash: N/A (64-bit)
Function Runtime Version: 4.25.3.21264
[2024-03-26T09:35:56.884Z] 0.01s - Debugger warning: It seems that frozen modules are being used, which may
[2024-03-26T09:35:56.888Z] 0.00s - make the debugger miss breakpoints. Please pass -Xfrozen_modules=off
[2024-03-26T09:35:56.893Z] 0.00s - to python to disable frozen modules.
[2024-03-26T09:35:56.897Z] 0.00s - Note: Debugging will proceed. Set PYDEVD_DISABLE_FILE_VALIDATION=1 to disable this validation.
[2024-03-26T09:35:57.750Z] Worker process started and initialized.
Functions:
BlobTrigger1: blobTrigger
For detailed output, run func with --verbose flag.
[2024-03-26T09:36:02.866Z] Host lock lease acquired by instance ID '000000000xxxxxxxxxx'.
[2024-03-26T09:36:30.129Z] Executing 'Functions.BlobTrigger1' (Reason='New blob detected(LogsAndContainerScan): abc360/sqlite_db_file/ABC100102_2_20230202.db', Id=fb47edb3-xxxxxxxxxxxxxxxx)
[2024-03-26T09:36:30.133Z] Trigger Details: MessageId: 109019e4-xxxxxxxxxxx, DequeueCount: 1, InsertedOn: 2024-03-26T09:36:28.000+00:00, BlobCreated: 2024-03-26T09:36:26.000+00:00, BlobLastModified: 2024-03-26T09:36:26.000+00:00
[2024-03-26T09:36:30.244Z] Connected to SQLite database successfully
[2024-03-26T09:36:30.244Z] Processing blob: ABC100102_2_20230202.db
[2024-03-26T09:36:30.244Z] Tables in SQLite file: [('users',), ('orders',)]
[2024-03-26T09:36:30.289Z] Executed 'Functions.BlobTrigger1' (Succeeded, Id=fb47edb3xxxxxxxxxxxx, Duration=925ms)