简体   繁体   English

AWS Lambda function 无法从 S3 连接和查询 SQLite 数据库文件

[英]AWS Lambda function can't connect to and query SQLite DB file from S3

My goal is to upload a SQLite database file to AWS S3, use AWS Lambda & python ( sqlite3 ) to connect to the database, query it, and return some of its data.我的目标是将 SQLite 数据库文件上传到 AWS S3,使用 AWS Lambda & python ( sqlite3 ) 连接到数据库,查询它,并返回它的一些数据。

Having uploaded the database file to S3, I wrote a python script (which is to become the Lambda function) that successfully downloads the database from S3, connects to it with sqlite3 , and returns some query results.将数据库文件上传到 S3 后,我编写了一个 python 脚本(将成为 Lambda 函数)成功地从 S3 下载数据库,使用sqlite3连接到它,并返回一些查询结果。

The issue is that when I take the exact same code and put it in AWS Lambda, I get the following error:问题是,当我采用完全相同的代码并将其放入 AWS Lambda 时,出现以下错误:

{
  "errorMessage": "malformed database schema (message_idx_undelivered_one_to_one_imessage) - near \"where\": syntax error",
  "errorType": "DatabaseError",
  "stackTrace": [
    "  File \"/var/task/lambda_function.py\", line 11, in lambda_handler\n    print(cursor.execute(\"select mycolumn from message limit 1\").fetchone())\n"
  ]
}

Here is the python script/Lambda function:这是 python 脚本/Lambda function:

import sqlite3
import boto3

def lambda_handler(event, context):
    s3 = boto3.resource("s3")
    bucket = s3.Bucket("my-bucket")
    bucket.download_file("my_database.db", "/tmp/my_database.db")

    conn = sqlite3.connect("/tmp/my_database.db")
    cursor = conn.cursor()
    print(cursor.execute("select mycolumn from message limit 1").fetchone())

Running this file locally works correctly.在本地运行此文件可以正常工作。 I have confirmed that both my local environment and Lambda are using these versions of the following:我已经确认我的本地环境和 Lambda 都在使用以下这些版本:

boto3: 1.20.32
python: 3.9.13
sqlite3: 2.6.0

Here is the schema of the database I'm trying to read.是我正在尝试读取的数据库的架构。

Why is Lambda producing this error while my local environment isn't?为什么 Lambda 会产生此错误,而我的本地环境却不会? How can I go about getting python to connect to the database within the Lambda function?请问go如何获取python连接到Lambda function内的数据库?

From pastebin extract here is your DDL in your database schema which triggers your error when opening your database从 pastebin 提取这里是你的数据库模式中的 DDL,它在打开你的数据库时触发你的错误

CREATE INDEX message_idx_undelivered_one_to_one_imessage
ON message(
    cache_roomnames,
    service,
    is_sent,
    is_delivered,
    was_downgraded,
    item_type
) where cache_roomnames IS NULL
    AND service = 'iMessage'
    AND is_sent = 1
    AND is_delivered = 0
    AND was_downgraded = 0
    AND item_type == 0;

The partial index feature has been integrated in sqlite since the 3.8.0 version https://www.sqlite.org/partialindex.html . sqlite自3.8.0版本https://www.sqlite.org/partialindex.html开始集成了部分索引功能。 The sqlite3 version on the lambda environment is probably to old to handle such king of schema. lambda 环境中的 sqlite3 版本可能太旧,无法处理这种模式之王。 An version bump in your sqlite3 dependency in your lambda environment should fix your issue. lambda 环境中的 sqlite3 依赖项中的版本提升应该可以解决您的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 AWS Lambda 函数从 S3 解码 a.gz 文件? - How can I decode a .gz file from S3 using an AWS Lambda function? AWS Lambda 无法从 same.network 中的 Amazon S3 存储桶检索文件 - AWS Lambda can't retrieve file from an Amazon S3 Bucket in same network function 在从 aws 中的 s3 读取文件后将数据传递给 aws cognito 时退出 lambda 在 nodejs 中 - function exits when passing data to aws cognito after reading file from s3 in aws lambda in nodejs 使用上传到 S3 的 json 文件中的输入参数触发 AWS lambda function - Trigger AWS lambda function using input parameter from json file uploaded to S3 AWS Amplify S3 受保护文件 - Lambda Function 调用 - AWS Amplify S3 Protected File - Lambda Function Invocation 使用aws下载s3 bucket文件 lambda function - download s3 bucket file using aws lambda function 从 AWS lambda function 中的 s3 存储桶中读取 .mdb 或 .accdb 文件并使用 python 将其转换为 excel 或 csv - Reading .mdb or .accdb file from s3 bucket in AWS lambda function and converting it into excel or csv using python 从 lambda function 读取 AWS S3 内的 excel 文件时超时 - timeout when reading excel file inside AWS S3 from lambda function 根据将文件从一个 S3 存储桶复制到另一个存储桶的清单文件触发 AWS Lambda function - Trigger AWS Lambda function based on a manifest file which copies files from one S3 bucket to another AWS Lambda function 写入 S3 - AWS Lambda function write to S3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM