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
질문됨 2년 전574회 조회
2개 답변
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
전문가
답변함 2년 전
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
답변함 2년 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠