无法封送响应:datetime.datetime(2019,5,29,13,41,46,937000,tzinfo = tzlocal())不可序列化为JSON”,

时间:2019-05-30 11:07:04

标签: python amazon-web-services aws-lambda amazon-kinesis

我正在尝试为代码中的ON_DEMAND kinesis视频传递时间,但出现此错误:

"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']}

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

1 个答案:

答案 0 :(得分:0)

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