【问题标题】:How to upload video to s3 using API GW and python?如何使用 API GW 和 python 将视频上传到 s3?
【发布时间】:2020-11-15 16:46:27
【问题描述】:

我正在尝试制作一个将视频上传到 s3 的 api。我已经准备好设法在 s3 中上传视频,但问题是视频文件无法正常工作。我检查了视频文件的内容类型,它是 binary/octet-stream 而不是 video/mp4 。所以我在调用 put_object api 时将 content-type 设置为“video/mp4”,但它仍然无法正常工作。

我使用 Lambda 函数将视频放到 s3 中。这是我的 lambda 代码 -

import json
import base64
import boto3



def lambda_handler(event, context):
    

    bucket_name = 'ad-live-streaming'
    s3_client = boto3.client('s3')
    
    file_content = event['content']
    merchantId = event['merchantId']
    catelogId = event['catelogId']
    file_name = event['fileName']
    
    file_path = '{}/{}/{}.mp4'.format(merchantId, catelogId, file_name)
 

    s3_response = s3_client.put_object(Bucket=bucket_name, Key=file_path, Body=file_content, ContentType='video/mp4')    

        
    return {
        'statusCode': 200,
        "merchantId":merchantId,
        "catelogId":catelogId,
        "file_name":file_name,
    }

知道如何解决这个问题吗?

【问题讨论】:

    标签: python-3.x amazon-web-services amazon-s3 aws-lambda aws-api-gateway


    【解决方案1】:

    根据Upload binary files to S3 using AWS API Gateway with AWS Lambda | by Omer Hanetz | The Startup | Medium 中的示例,看来您需要从base64 解码文件:

    file_content = base64.b64decode(event['content'])
    

    【讨论】:

    • 也许本文的其余部分会有所帮助。
    • 实际上我从那篇文章中得到了帮助。我遵循了每一步....但到目前为止没有运气:(我在我的 django 服务器上使用了相同的 lambda 代码,它运行良好,所以我假设视频文件通过 API GW 时发生了一些事情。
    • 您可以尝试先上传一个简单的文本文件(而不是视频),以便检查内容是否正确上传。
    猜你喜欢
    • 2017-08-05
    • 2014-08-06
    • 2011-05-13
    • 1970-01-01
    • 2012-10-08
    • 2019-10-26
    • 1970-01-01
    • 1970-01-01
    • 2018-06-24
    相关资源
    最近更新 更多