当在AWS Lambda中导入图像时,需要将图片文件先转为base64编码,然后在代码中引用编码后的字符串。以下是一个示例代码:
import json
import base64
def lambda_handler(event, context):
with open("image.png", "rb") as image_file:
encoded_string = base64.b64encode(image_file.read())
print("base64 encoded image: ", encoded_string)
return {
'statusCode': 200,
'body': json.dumps('Hello from Lambda!')
}
在以上示例代码中,我们首先使用Python中的open()
函数打开图片文件,然后通过base64.b64encode()
函数将图片文件转成base64编码。最后在代码中以字符串形式引用编码后的图片。
请注意,在将图片文件转为base64编码时,可能需要将文件中不必要的信息(如图片格式头信息)去掉,具体方法请参考base64
模块文档。
如果以上方法仍然无法解决问题,建议查看AWS Lambda运行日志以获取更详细的错误提示。