0

I have this code in node.js. I just research this one and I want to convert this to python.

This code is to put signed URL on .m3u8 manifest file.

Does anyone know how to do this? I'm using AWS Lambda function.

Converting this node.js code to python.

const aws = require('aws-sdk');
const s3 = new aws.S3({ apiVersion: '2006-03-01' });
const bucketName = 'streaming-files';

exports.lambdaHandler = async (event, context, callback) => {
    const request = event.Records[0].cf.request;

    // Get the key name of S3 object
    const key = request.uri.substring(1);
    const s3Params = {
        Bucket: bucketName,
        Key: key
    };

    // Get the contents of the raw manifest file from the S3 bucket
    const s3Res = await s3.getObject(s3Params).promise();
    const srcBody = s3Res.Body.toString('utf-8');

    // rewriting process
    const qs = request.querystring;
    const signedBody = srcBody.replace(/\.m3u8/g, `.m3u8?${qs}`).replace(/\.ts/g, `.ts?${qs}`);

    // Add Respose header for content-type and CORS
    const response = {
        status: 200,
        statusDescription: 'OK',
        headers: {
            'content-type': [{
                key: 'Content-Type',
                value: 'application/x-mpegURL'
            }],
            'access-control-allow-methods': [{
                key: 'Access-Control-Allow-Methods',
                value: 'GET,HEAD'
            }],
            'access-control-allow-origin': [{
                key: 'Access-Control-Allow-Origin',
                value: '*'
            }]
        },
        body: signedBody,
    };
    callback(null, response);
};

Here's my python code. But I got stuck on getting the key name and also on rewriting process.

import json
import boto3

s3 = boto3.client('s3')

BUCKET_NAME = "streaming-files"

def lambda_handler(event, context):
    request = event['Records'][0]['cf']['request']

    # Get the key name of S3 object
    key = request['uri'];
    s3Res = s3.get_object(Bucket=BUCKET_NAME, Key=key)

    # Get the contents of the raw manifest file from the S3 bucket
    srcBody = s3Res['Body'].read().decode('utf-8')

    # rewriting process
    qs = request['querystring']
    signedBody = srcBody.replace(/\.m3u8/g, `.m3u8?${qs}`).replace(/\.ts/g, `.ts?${qs}`)

    response = {
        'status':  200,
        'statusDescription':  "OK",
        'headers': {
            'content-type': [
                {
                    'key': 'Content-Type',
                    'value': 'application/x-mpegURL'
                }
            ],
            'access-control-allow-methods': [
                {
                    'key': 'Access-Control-Allow-Methods',
                    'value': 'GET,HEAD'
                }
            ],
            'access-control-allow-origin': [
                {
                    'key': 'Access-Control-Allow-Origin',
                    'value': '*'
                }
            ]
        },
        'body': ''
    }
    return response

This code has syntax error:

const signedBody = srcBody.replace(/\.m3u8/g, `.m3u8?${qs}`).replace(/\.ts/g, `.ts?${qs}`);

I'm stuck with these. How can I convert or what is the equivalent of these codes to python?

const key = request.uri.substring(1);
const signedBody = srcBody.replace(/\.m3u8/g, `.m3u8?${qs}`).replace(/\.ts/g, `.ts?${qs}`);
John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
Codeblooded Saiyan
  • 1,457
  • 4
  • 28
  • 54

0 Answers0