Serverless Python Webhooks handler on AWS with Lambda Function URLs
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.
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.
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)
Relevant questions
Transfer files (1GB to 2G) from web url to S3 on a schedule
Accepted Answerasked 3 years agoHandler error python lambda function
asked 3 months agoHow do I query X-ray using a http URL, where that URL is not the first part of the request?
asked 2 months agocan we attach the custom domain to lambda function urls ?
Accepted Answerasked 2 months agoWhen to invoke a lambda directly or via API Gateway
asked 6 months agoServerless Python Webhooks handler on AWS with Lambda Function URLs
asked a month agoTrying to call API with a list of URLs but Lambda is timing out
asked 24 days agocreate a gallery of video
asked 6 months agosqs event triggers lambda directly, is there a way to delay that execution by 10-20 seconds?
Accepted Answerasked 2 years agoReturn Value from Lambda function triggered by SQS to individual client
Accepted Answerasked 5 months ago