python - 无法使用 s3 触发器将 s3 对象传输到 rds
问题描述
我有下面的 lambda 函数代码,它将对象从 s3 存储桶传输到 AWS RDS 数据库。
import json
import boto3
import pymysql
s3_client = boto3.client('s3')
def lambda_handler(event, context):
bucket_name = event["bucket"]
s3_file_name = event["object"]
resp = s3_client.get_object(Bucket=bucket_name, Key=s3_file_name)
data = resp['Body']
rds_endpoint = ""
username = #username for RDS Mysql
password = # RDS Mysql password
db_name = # RDS MySQL DB name
conn = None
try:
conn = pymysql.connect(host=rds_endpoint, user=username, password=password, database=db_name)
except pymysql.MySQLError as e:
print("ERROR: Unexpected error: Could not connect to MySQL instance.")
try:
cur = conn.cursor()
cur.execute(#db stuff)
conn.commit()
except Exception as e:
print(e)
return 'Table not created!'
with conn.cursor() as cur:
try:
cur.execute(#db stuff)
conn.commit()
output = cur.execute()
except:
output = ("Entry not inputted! Error!")
print("Deleting the csv file from s3 bucket")
return {
'statusCode': 200,
'body': 'Successfully uploaded!'
}
上面的代码适用于这个给定的测试 ev:
{“桶”:“蟒蛇桶”,“对象”:“bobmarley.mp3”}
但是,当我尝试通过将代码行更改为下面的代码行来使其适应 s3 存储桶时,如本教程中所示:https ://www.data-stats.com/s3-data-ingestion-to-rds-through- λ/
bucket_name = event["Records"][0]["s3"]["bucket"]["name"]
s3_file_name = event["Records"][0]["s3"]["object"]["key"]
我收到此错误:
[ERROR] TypeError: list indices must be integers or slices, not str
Traceback (most recent call last):
File "/var/task/lambda_function.py", line 7, in lambda_handler
bucket_name = event["Records"]["s3"]["bucket"]["name"]
解决方案
推荐阅读
- apache-spark - python的火花流在databricks中不起作用
- javascript - Javascript 中最长的公共前缀
- python - 在pyfcm中推送通知后无法在firebase控制台中找到通知
- python - 缓慢的刮擦和不拉动线程中的所有页面 - 需要帮助
- unity3d - 创建 3D 轴以移动对象(在 Unity 中)
- cucumber - 是否有任何用于维护和管理黄瓜功能文件的最佳 JIRA 服务器插件/应用程序?
- html - 使 flex-box 中的元素占用动态空间量(如果两个元素,则为 50%,如果三个元素,则为 33%,等等)
- javascript - 如何使用相对于画布的 x、y 坐标在画布上绘图
- arrays - 通过 VBA 将工作表添加到数组
- active-directory - Kerberos 新用户凭证流