
[英]python - 'utf-8' codec can't decode bytes in position 0-2: invalid continuation byte
[英]Unable to marshal response: 'utf-8' codec can't decode byte 0xc8 in position 48: invalid continuation byte
我正在尝试在 AWS Lambda 上使用此代码,它显示此错误: Unable to marshal response: 'utf-8' codec can't decode byte 0xc8 in position 48: invalid continuation byte
,我试图在 True 之间更改 isBase64Encoded和 False 但错误仍然发生,不知道。
import requests
def lambda_handler(event, context):
video_data = requests.get("http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ElephantsDream.mp4", stream=True)
video_content = video_data.content
response = {
"statusCode": 200,
"headers": {
"Content-Type": "video/mp4",
"Content-Length": len(video_content)
},
"body": video_content,
"isBase64Encoded": False
}
return response
完整错误:
Response
{
"errorMessage": "Unable to marshal response: 'utf-8' codec can't decode byte 0xc8 in position 48: invalid continuation byte",
"errorType": "Runtime.MarshalError",
"stackTrace": []
}
Function Logs
START RequestId: 40419a51-2e14-4035-81f5-7f135131a2ac Version: $LATEST
[ERROR] Runtime.MarshalError: Unable to marshal response: 'utf-8' codec can't decode byte 0xc8 in position 48: invalid continuation byte
Traceback (most recent call last):END RequestId: 40419a51-2e14-4035-81f5-7f135131a2ac
REPORT RequestId: 40419a51-2e14-4035-81f5-7f135131a2ac Duration: 1654.01 ms Billed Duration: 1655 ms Memory Size: 1024 MB Max Memory Used: 373 MB Init Duration: 240.18 ms
Request ID
40419a51-2e14-4035-81f5-7f135131a2ac
也许那是因为你必须对视频进行编码
import base64
encoded_video_content = base64.b64encode(video_content).decode()
然后在响应中将其更改为以下内容
response = {
"statusCode": 200,
"headers": {
"Content-Type": "video/mp4"
},
"body": encoded_video_content,
"isBase64Encoded": True
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.