如何使用AWS将node.js代码转换为pythonLamda@Edge

2024-05-14 16:19:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我在node.js中有这段代码。我只是研究这个,我想把它转换成python

此代码用于将签名的URL放在.m3u8清单文件上

有人知道怎么做吗?我正在使用AWS Lambda函数

将此node.js代码转换为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);
};

这是我的python代码。但我在获取密钥名和重写过程中遇到了困难

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

此代码有语法错误:

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

我受不了这些。 如何将这些代码转换为python,或者这些代码的等价物是什么

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

Tags: thekey代码events3valuerequestreplace

热门问题