简体   繁体   English

如何使用 API GW 和 python 将视频上传到 s3?

[英]How to upload video to s3 using API GW and python?

Im trying to make a api which will upload video to s3.我正在尝试制作一个 api ,它将视频上传到 s3。 I all ready managed to upload the video in s3, but the problem is the video file is not working.我已经准备好设法在 s3 中上传视频,但问题是视频文件无法正常工作。 i checked content-type of video file, and it's binary/octet-stream instead on video/mp4.我检查了视频文件的内容类型,它是二进制/八位字节流,而不是视频/mp4。 So i set content-type to "video/mp4" while calling put_object api, but it still not working.所以我在调用 put_object api 时将内容类型设置为“video/mp4”,但它仍然无法正常工作。

I use Lambda function for putting the video to s3.我使用 Lambda function 将视频放入 s3。 here is my lambda code -这是我的 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,
    }

Any idea how to solve this issue?知道如何解决这个问题吗?

Based on the example in Upload binary files to S3 using AWS API Gateway with AWS Lambda |基于使用 AWS API 网关和 AWS Lambda 将二进制文件上传到 S3 中的示例 | by Omer Hanetz | 通过奥马尔哈内茨 | The Startup | 创业 | Medium , it appears that you need to decode the file from base64: ,看来您需要从 base64 解码文件:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM