我正在尝试使用python从Lambda函数更新Redshift。为此,我尝试合并2个代码片段。当我分别运行它们时,这两个片段都起作用。
import psycopg2 conn_string = "dbname='name' port='0000' user='name' password='pwd' host='url'" conn = psycopg2.connect(conn_string) cursor = conn.cursor() cursor.execute("UPDATE table SET attribute='new'") conn.commit() cursor.close()
from __future__ import print_function import json import urllib import boto3 print('Loading function') s3 = boto3.client('s3') def lambda_handler(event, context): #print("Received event: " + json.dumps(event, indent=2)) # Get the object from the event and show its content type bucket = event['Records'][0]['s3']['bucket']['name'] key = urllib.unquote_plus(event['Records'][0]['s3']['object']['key']).decode('utf8') try: response = s3.get_object(Bucket=bucket, Key=key) print("CONTENT TYPE: " + response['ContentType']) return response['ContentType'] except Exception as e: print(e) print('Error getting object {} from bucket {}. Make sure they exist and your bucket is in the same region as this function.'.format(key, bucket)) raise e
由于这两个段都起作用,因此我尝试将它们组合在一起,以便在将文件上传到s3时可以更新Redshift:
from __future__ import print_function import json import urllib import boto3 import psycopg2 print('Loading function') s3 = boto3.client('s3') def lambda_handler(event, context): #print("Received event: " + json.dumps(event, indent=2)) # Get the object from the event and show its content type bucket = event['Records'][0]['s3']['bucket']['name'] key = urllib.unquote_plus(event['Records'][0]['s3']['object']['key']).decode('utf8') conn_string = "dbname='name' port='0000' user='name' password='pwd' host='url'" conn = psycopg2.connect(conn_string) cursor = conn.cursor() cursor.execute("UPDATE table SET attribute='new'") conn.commit() cursor.close() try: response = s3.get_object(Bucket=bucket, Key=key) print("CONTENT TYPE: " + response['Body'].read()) return response['Body'].read() except Exception as e: print(e) print('Error getting object {} from bucket {}. Make sure they exist and your bucket is in the same region as this function.'.format(key, bucket)) raise e
由于我使用的是外部库,因此需要创建一个部署程序包。我创建了一个新文件夹(lambda_function1)并将我的.py文件(lambda_function1.py)移至该文件夹。我运行以下命令在该文件夹中安装psycopg2:
pip install psycopg2 -t \lambda_function1
我收到以下反馈:
Collecting psycopg2 Using cached psycopg2-2.6.1-cp34-none-win_amd64.whl Installing collected packages: psycopg2 Successfully installed psycopg2-2.6.1
然后,我压缩目录的内容。并将该zip文件上传到我的lambda函数。当我将文档上传到功能监视的存储桶时,我的cloudwatch日志中收到以下错误:
Unable to import module 'lambda_function1': No module named _psycopg
当我在库中查找时,唯一名为“ _psycopg”的是“ _psycopg.pyd”。
是什么导致此问题?当我使用3.4时,Lambda使用Python 2.7是否重要?在Windows计算机上压缩文件内容是否重要?有没有人能够从lambda成功连接到Redshift?
为了使其正常工作,您需要psycopg2使用静态链接libpq.so库进行构建。查看此仓库https://github.com/jkehler/awslambda- psycopg2。它已经构建了psycopg2软件包并说明了如何自行构建。
psycopg2
libpq.so
回到您的问题:
是什么导致此问题?
psycopg2 需要使用用于Linux的静态链接库来构建编译器。
当我使用3.4时,Lambda使用Python 2.7是否重要?
是的,lambda仅支持2.7版本。只需创建虚拟环境并在其中安装所有必需的软件包即可。
在Windows计算机上压缩文件内容是否重要?
只要您压缩的所有库都可以在Linux上运行,它就不会
有没有人能够从lambda成功连接到Redshift?
是。