python - 无法编码响应 : datetime. datetime(2019, 5, 29,

对于 ON_DEMAND kinesis video,我正在尝试按照代码中所示的方式传递时间,但出现此错误:

"errorMessage": "Unable to marshal response: datetime.datetime(2019, 5, 29, 13, 41, 46, 937000, tzinfo=tzlocal()) is not JSON serializable",
import json
import boto3, datetime
from botocore.exceptions import  ClientError

dynamodb = boto3.resource('dynamodb')

def lambda_handler(event, context):

        try:


            # for live video streaming getting HLS URL LIVE and ON DEMAND

            live_data_end_point= boto3.client('kinesisvideo', region_name ="us-east-1").get_data_endpoint(
                  StreamName=event["device_name"],
                  APIName='GET_HLS_STREAMING_SESSION_URL'
                   )
            live_data_end_point = live_data_end_point["DataEndpoint"]

            if event["mode"]=="LIVE":
                media = boto3.client('kinesis-video-archived-media',region_name = 'us-east-1', endpoint_url=live_data_end_point)
                url_response = media.get_hls_streaming_session_url(

                StreamName=event["device_name"],
                PlaybackMode='LIVE',
                DiscontinuityMode='ALWAYS',
                Expires=1000,
                MaxMediaPlaylistFragmentResults=123,
                )

                hls_url = url_response['HLSStreamingSessionURL']
                hls_live_medial_url = [{'hls_url':hls_url}]
            else:
                media = boto3.client('kinesis-video-archived-media',region_name = 'us-east-1', endpoint_url=live_data_end_point)
                url_response = media.get_hls_streaming_session_url(

                StreamName=event["device_name"],
                PlaybackMode='ON_DEMAND',
                HLSFragmentSelector={
                'FragmentSelectorType':'SERVER_TIMESTAMP',
                'TimestampRange': {
                    'StartTimestamp':datetime.datetime(2019, 5, 29, 13, 41),
                    'EndTimestamp':  datetime.datetime(2019, 5, 29, 13, 55)
                }
                  },
                DiscontinuityMode='ALWAYS',
                Expires=1000,
                MaxMediaPlaylistFragmentResults=500,
                )

                hls_url = url_response['HLSStreamingSessionURL']
                hls_live_medial_url = [{'hls_url':hls_url}]



            # for getting list of all fragments:


            end_point_list_frag = boto3.client('kinesisvideo', region_name ="us-east-1").get_data_endpoint(
                  StreamName=event["device_name"],
                  APIName='LIST_FRAGMENTS'
                   )

            end_point_list_frag = end_point_list_frag["DataEndpoint"]
            client_frag =  boto3.client('kinesis-video-archived-media', region_name = "us-east-1", endpoint_url = end_point_list_frag)

            response = client_frag.list_fragments(
                StreamName=event["device_name"],
                MaxResults=123,
                FragmentSelector={
               'FragmentSelectorType': 'SERVER_TIMESTAMP',
               'TimestampRange': {
               'StartTimestamp': datetime.datetime(2019, 5, 29, 13, 41),
               'EndTimestamp': datetime.datetime(2019, 5, 29, 13, 55)
               }
               }
               )

            fragments = response['Fragments']


            # listing all videos available for that devices by using get_media_for_fragment_list



            return {
             'ResponseCode': 200,
             'hls_url': hls_live_medial_url,

             'fragment_list':response,

              }

        except ClientError as ex:
            return {'Result':ex.response['Error']}

##############################################

最佳答案

使用 datetime.srtptime(datetime_object, format) 将您的日期解析为正确的格式。

关于python - 无法编码响应 : datetime. datetime(2019, 5, 29, 13, 41, 46, 937000, tzinfo=tzlocal()) 不是 JSON 可序列化的”,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56377066/

相关文章:

node.js - 在 Nexus 中获取最新版本的 Node 包

google-sheets - 具有日期条件的 Google 表格摘要

amazon-web-services - AWS ECR 如何限制除最新标签之外的重新标记

c# - 从不同的项目 .Net 访问 protobuf

python-3.x - 如何在 Python 中创建具有每个变量值之间差距的堆积条形图

reactjs - 我们如何在使用 react 最终形式时在组件中设置状态

python - Jupyter 笔记本 : how to run python shell com

javascript - 当我在滚动功能上添加动画时,导航栏熄灭

docker - 无法使用 docker login --password-stdin

google-drive-api - 从 Google Drive "Computers"获取文件到