Serverless Python Webhooks handler on AWS with Lambda Function URLs

0

I need to upload a video file, which is available via the download URL of a webhook event. I am looking for a solution to directly put/stream a video file into an S3 bucket using a Python webhooks handler lambda URL function. Is this possible? Here is the script:

import certifi
import boto3
import urllib3

def lambda_handler(event, context):
    body = json.loads(event["body"])
    recording_files = body["payload"]["object"]["recording_files"]
    download_token = body["download_token"]
    bucket = 'tstbucket00'  #s3 bucket
    upload_download_key = 'folder/folder' #desired s3 path or filename
    
    for idx in range(len(recording_files)):
       recording_files[idx]= recording_files[idx]["download_url"] +'?access_token=' + download_token
       s3=boto3.client('s3',aws_access_key_id=y_access_key,  aws_secret_access_key=y_secret_key)
       http=urllib3.PoolManager(cert_reqs='CERT_REQUIRED',ca_certs=certifi.where())
       get_recording = http.request('GET', recording_files[idx]l,preload_content=False)
       s3.upload_fileobj(get_recording, bucket, upload_download_key)

The about script successfully creates a folder in the s3 bucket but does not directly stream the mp4 file into s3 bucket.

fit-dev
asked 2 years ago564 views
2 Answers
0

I think the issue is that you're passing the entire get_recording object to upload_fileobj where you should be passing the data. Perhaps the final line should be s3.upload_fileobj(get_recording.data, bucket, upload_download_key) ?

Also: Where you're creating the s3 object you don't need to specify the access and secret keys if the Lambda function has the appropriate IAM role attached to it. You can just do s3 = boto3.client('s3') which is much simpler and means you don't have to set up the credentials yourself.

profile pictureAWS
EXPERT
answered 2 years ago
0

Thank you so much for your thoughtful answer @Brettski@AWS! It really helped me figure out the issue. I am a newbie to working with s3 and you've made the development experience amazing. That side, it turns out that the script was uploading as expected. I just needed to add the .mp4 file extension to the filename. Here is the snippet I used in my testing in the terminal:

zoom_access_token = " Your_Token"
download_url = "Your_download_URL"
new_download_url = download_url +'?access_token=' + zoom_access_token
bucket = 'Yourbucket00' #your s3 bucket
upload_download_key = 'folder/folder.mp4' #your desired s3 path or filename

s3=boto3.client('s3',aws_access_key_id=y_access_key, aws_secret_access_key=y_secret_key)
http=urllib3.PoolManager(cert_reqs='CERT_REQUIRED',ca_certs=certifi.where())
get_recording = http.request('GET', new_download_url,preload_content=False)
s3.upload_fileobj(get_recording, bucket, upload_download_key)
fit-dev
answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions