我是后端开发和 Python 的初学者。我已经在 RDS 中插入了存储在 S3 存储桶中的图像链接,现在我在 lambda python 函数中对该表运行查询,它按预期返回一个字符串,我如何使该 lambda 函数使用该链接返回图像。下面是我的 lambda 函数
#!/usr/bin/python
import sys
import logging
import pymysql
import json
rds_host="host"
name="name"
password="passwdord"
db_name="DB"
port = 3306
def save_events(event):
"""
This function fetches content from mysql RDS instance
"""
result = []
conn = pymysql.connect(rds_host, user=name, passwd=password, db=db_name, connect_timeout=30)
with conn.cursor() as cur:
cur.execute("SELECT * FROM bodyPart")
for row in cur:
result.append(list(row))
print ("Data from RDS...")
print (result)
cur.close()
print(json.dumps({'bodyParts':result}))
def lambda_handler(event, context):
save_events(event)
return result
下面是该函数的输出:
Response:
[
[
1,
"Chest",
"s3://imagename"
],
[
2,
"Shoulder",
null
],
[
3,
"Arms",
null
]
]
请您参考如下方法:
由于您来自 RDS 的响应包含存储桶名称和文件名,我将只使用 boto3 来获取文件。您需要向您的 lambda 添加相关权限才能访问您的存储桶。
例子:
import boto3
s3 = boto3.client('s3')
my_bucket = <your bucket name goes here>
my_file = <your file path goes here>
image = s3.get_object(Bucket='my-bucket', Key='my_file')
获取存储桶名称和文件名只需要一点字符串操作。
