skip to Main Content

I tried to create simple yt converter which from given url will download audio and upload it to s3. I was able to save text file using this lambda function but when it comes to audio it just get timed out. Here is my code:

import json
import tinys3
from pytube import YouTube
import boto3

def lambda_handler(event, context):
    video_url = event['url'] 
    file_name = video_url[8:]+'.mp4'
    BUCKET_NAME = 'my_bucket_name'
    
    def YoutubeAudioDownload(video_url,destination_path):
        AUDIO_DOWNLOAD_DIR = destination_path
        video = YouTube(video_url)
        audio = video.streams.filter(only_audio = True).first()
        try:
            audio.download(AUDIO_DOWNLOAD_DIR)
            print("audio was downloaded successfully")
        except:
            print("Failed to download audio")
    
    try:
    
        YoutubeAudioDownload(video_url,'/tmp/'+ file_name)
        s3 = boto3.client('s3')
        response = s3.put_object(Body='/tmp/'+ file_name, Bucket=BUCKET_NAME, Key=file_name, ContentType='audio/mp4')
        print(response)
        return {
            'statusCode': 200,
            'body': json.dumps('MP4 file uploaded successfully')
        }
    except Exception as e:
        print(e)
        return {
            'statusCode': 500,
            'body': json.dumps('Error uploading MP4 file: ' + str(e))
        }

When I delete extension ‘.mp4’ from file name it works but uploaded file isn’t working and weight only 39B. When extension is added there is timeout all the time.

2

Answers


  1. Downloading videos and uploading to S3 might take some time. Ensure your Lambda function has sufficient execution time; otherwise, it might be interrupted due to a timeout. You can set the timeout duration in Lambda’s configuration under General Configuration.

    Login or Signup to reply.
  2. Your issue seems to be you are mising writing binary data to S3 using the Python client. That is, you need to read the binary data from the downloaded MP4 file and then upload that binary data to S3. You can use the open function to read the file.

    I just tested this code and it works. Only the upload part is specified. I uploaded a MP4 named Friends.mp4.

    import json
    import boto3
    
    def upload_mp4_to_s3():
        BUCKET_NAME = '<My Bucket>'  # Update with your S3 bucket name
        s3 = boto3.client('s3')
    
        try:
            file_name = 'Friends.mp4'
    
            with open('C:\AWS\Friends.mp4', 'rb') as f:
                s3.upload_fileobj(f, BUCKET_NAME, file_name, ExtraArgs={'ContentType': 'video/mp4'})
    
            print("MP4 file uploaded successfully")
    
        except Exception as e:
            print(e)
    
    if __name__ == "__main__":
        upload_mp4_to_s3()
    

    This code does the same as this Java example.

    public void putObject(byte[] data, String bucketName, String objectKey) {
            s3 = getClient();
            try {
                s3.putObject(PutObjectRequest.builder()
                    .bucket(bucketName)
                        .key(objectKey)
                        .build(),
                    RequestBody.fromBytes(data));
    
            } catch (S3Exception e) {
                System.err.println(e.getMessage());
                System.exit(1);
            }
        }
    

    Point here is when using AWS SDK, you must specify the binary data to upload.

    After I ran the Python code — the file is located in the bucket.

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search