Continuing our series on creating a serverless Video on Demand (VoD) system, we now focus on the critical aspect of video uploading. Specifically, we'll explore how to securely upload videos to an Amazon S3 bucket using a signed URL. This process involves the use of AWS API Gateway and Lambda to generate the URL, ensuring both security and efficiency.
Uploading directly to an API server, then having it transfer the file to S3, is a resource-intensive process that can strain your infrastructure. This was a lesson learned the hard way, resulting in significant challenges. To avoid these pitfalls, we will implement a direct upload method to S3, bypassing the API Gateway's 10MB payload limit and enhancing overall architecture efficiency.
The series is structured as follows:
Part 1 - Architecture Overview: We lay the groundwork, detailing the architecture and AWS services involved in our serverless VoD solution.
Part 2 - Secured File Upload: We'll cover the video upload process, how to securely upload to s3 bucket using signed url
Part 3 - Converting to Stream Format: This part will explain how to manage events from s3 upload to trigger conversion job to transform them into streaming format
Part 4 - Saving, Retrieving, and Playing: We will go through the saving and retrieval of video links, then play them using HLS Player on Chrome
Part 5 - Securing the Streaming: The final part will focus on securing the application to ensure that only authenticated users can access the videos.
To implement this solution, we need to set up the following AWS resources:
Note: We will only be uploading mp4 files for simplicity of the tutorial
Here is an overview of the process
import jsonimport boto3import osfrom botocore import clientimport uuiddef lambda_handler(event, context):s3 = boto3.client('s3', config=client.Config(signature_version='s3v4'))bucket_name = 's3-bucket-name-created-above'object_name = f'{str(uuid.uuid4())}.mp4'expiration = 3600 # or your preferred timeoutpresigned_url = s3.generate_presigned_url('put_object',Params={'Bucket': bucket_name, 'Key': object_name},ExpiresIn=expiration)print(presigned_url)return {'statusCode': 200,'upload_url': presigned_url}
4. Make sure to replace the "bucket_name" inside the script to the s3 bucket name we created above
To facilitate the retrieval of the signed URL:
{"Version": "2012-10-17","Statement": [{"Sid": "Statement1","Effect": "Allow","Action": "s3:*","Resource": "arn:aws:s3:::<bucket-name>/*"}]}
Try it Again Now get another upload_url using Section 3 Step 8 then upload it using Section 3 Step 10. We should now get a Status 200 Ok Response and your file should now be inside your bucket
NOTE: If your postman is throwing signature does not match error then try using the postman on web, this issue happened to me on a some postman version on linux
By following these steps, we establish a secure, efficient method for uploading videos directly to S3, bypassing potential bottlenecks. This setup not only enhances the performance of your VoD service but also lays the groundwork for a robust, scalable architecture. Stay tuned for the next part of our series, where we will delve into handling conversion completion events.
Stay ahead of the curve with our cutting-edge tech guides, providing expert insights and knowledge to empower your tech journey.
Subscribe to get updated on latest and relevant career opportunities