英文:
GoneException when sending image from AWS lambda to local machine
问题
我正在使用Websocket通过API网关发送jpg图像。由于我需要在另一台机器上实时获取图像。
我遵循了这篇关于Websocket API设置的帖子,并添加了一个单独的路由sendMessage,该路由调用Lambda函数。
这是在Lambda上运行的代码示例:
import base64
import boto3
api_client = boto3.client('apigatewaymanagementapi', endpoint_url='https://../test')
while not loaded:
with open("/tmp/image.jpg", "rb") as image:
b64string = base64.b64encode(image.read())
firstpart, secondpart = b64string[:len(b64string)//2], b64string[len(b64string)//2:]
api_client.post_to_connection(ConnectionId=connectionId, Data=firstpart)
api_client.post_to_connection(ConnectionId=connectionId, Data=secondpart)
api_client.post_to_connection(ConnectionId=connectionId, Data="connected successfully!")
而这是在另一台机器上的代码:
from websockets.sync.client import connect
import base64
from PIL import Image
with connect("wss...") as websocket:
websocket.send(json.dumps({"action":"sendMessage","message":"scanning"}))
while (b64image_string_1:=websocket.recv()) != "connected successfully!":
b64image_string_2 = websocket.recv()
b64image_string = b64image_string_1 + b64image_string_2
f = io.BytesIO(base64.b64decode(b64image_string))
img = Image.open(f)
尽管图像最初会被发送,但在运行几秒钟后我会收到错误消息。
botocore.errorfactory.GoneException: 调用PostToConnection操作时发生错误 (GoneException)
我猜测我的Websocket设置可能是导致错误的原因,但我不确定如何修复它。
英文:
I am sending jpg images over API gateway using Websocket. As I need the image in real time on another machine.
I followed this post on the set up for the Websocket API and added a separate route sendMessage that calls the Lambda function
https://stackoverflow.com/questions/55594587/setup-a-basic-websocket-mock-in-aws-apigateway
This is an example of the code that is running on Lambda
import base64
import boto3
api_client = boto3.client('apigatewaymanagementapi',endpoint_url='https://../test')
while not loaded:
with open("/tmp/image.jpg", "rb") as image:
b64string = base64.b64encode(image.read())
firstpart, secondpart = b64string[:len(b64string)//2], b64string[len(b64string)//2:]
api_client.post_to_connection(ConnectionId=connectionId, Data=firstpart)
api_client.post_to_connection(ConnectionId=connectionId, Data=secondpart)
api_client.post_to_connection(ConnectionId=connectionId, Data="connected successfully!")
and this is on the machine
from websockets.sync.client import connect
import base64
from PIL import Image
with connect("wss...") as websocket:
websocket.send(json.dumps({"action":"sendMessage","message":"scanning"}))
while (b64image_string_1:=websocket.recv()) != "connected successfully!":
b64image_string_2 =websocket.recv()
b64image_string = b64image_string_1 + b64image_string_2
f = io.BytesIO(base64.b64decode(b64image_string))
img = Image.open(f)
Although the images will be sent over initially, I would get an error a few seconds after it runs.
botocore.errorfactory.GoneException: An error occurred (GoneException) when calling the PostToConnection operation:
I'm guessing that my set up for the websocket might be what's causing the error but I'm not sure how to fix it.
答案1
得分: 0
我收到的错误实际上来自机器本身,某种方式,字符串的顺序被来自网络套接字的其他偶发警告/错误消息打断,导致接收到的字符串顺序错误。
机器中的最后一行导致了错误,并停止了网络套接字连接。
img = Image.open(f)
Image.open() 无法从错误连接的字符串加载,导致了错误。
在添加对接收到的字符串顺序的检查后,错误消失了。
如果出现 GoneException,请检查您的代码是否存在可能中断并断开网络套接字连接的错误。
不确定这篇帖子是否有用,但我会留下它。
英文:
The error I got was actually from the machine itself, somehow the order of the strings was interrupted by other occasional warning/error messages from the web socket and that caused the order of the strings received to be wrong.
The final line in the machine was the one that caused the error and stopped the web socket connection.
img = Image.open(f)
Image.open() could not load from the wrongly concatenated string and caused an error.
The error was gone after adding checks for the order of strings received.
If you get a GoneException, check your code for error that might've interrupted and disconnected the web socket connection.
Not sure if this post will be useful but I'll just leave it up
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论