Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use AWS lambda to upload video into S3 with download URL

I want to upload video into S3 using AWS lambda function. This video is not available in my local computer. I have 'download URL'. I don't want to download it into my local computer and upload it into S3. I am looking for a solution to directly put this video file into S3 using lambda function. If I use buffer or streaming, I will consume lots of memory. Is there a better efficient solution for this?

I really appreciate your help.

like image 400
Dasu Avatar asked May 29 '17 02:05

Dasu


People also ask

Can Lambda upload file to S3?

Nowadays, there is a growing demand for serverless architecture, which makes uploading files to AWS S3 using API gateway with AWS Lambda (NodeJs) extremely useful. By simply following the above steps, you can make your own API to upload your files to S3 buckets on AWS.

How do I upload videos to Amazon S3?

In the Amazon S3 console, choose the bucket where you want to upload an object, choose Upload, and then choose Add Files. In the file selection dialog box, find the file that you want to upload, choose it, choose Open, and then choose Start Upload. You can watch the progress of the upload in the Transfer pane.

How do I download and upload to AWS S3?

Sign in to the AWS Management Console and open the Amazon S3 console at https://console.aws.amazon.com/s3/ . In the Buckets list, choose the name of the bucket that you want to upload your folders or files to. Choose Upload.


2 Answers

I had the same question and developed the following quick solution that is not dependent on the /tmp disk limits. It uses download stream as a file-like object.

Features:

  • No external python modules, using AWS Lambda Python 3.6 builtin boto3 and urllib3
  • Has chunked reading built in, good for downloading large file
  • Efficient connection and memory usage through urllib3 pool management
  • Using configurable upload_fileobj builtin multipart and threaded upload

    import boto3
    import botocore.vendored.requests.packages.urllib3 as urllib3
    
    def lambda_handler(event, context):
    
        url='http://yourdownloadurl/file.tgz' # put your url here
        bucket = 'aws-s3-bucket' #your s3 bucket
        key = 'folder/filename' #your desired s3 path or filename
    
        s3=boto3.client('s3')
        http=urllib3.PoolManager()
        s3.upload_fileobj(http.request('GET', url,preload_content=False), bucket, key)
    
like image 152
Reza Hashemi Avatar answered Oct 03 '22 18:10

Reza Hashemi


You could certainly write an AWS Lambda function that would:

  • Download the file from the URL and store it in /tmp
  • Upload to Amazon S3 using the AWS S3 SDK

It would be easiest to download the complete file rather than attempting to stream it in 'bits'. However, please note that there is a limit of 500MB of disk space available for storing data. If your download is larger than 500MB, you'll need to do some creative programming to download portions of it and then upload it as a multi-part upload.

As for how to download it, use whatever library you prefer for download a web file.

like image 38
John Rotenstein Avatar answered Oct 03 '22 20:10

John Rotenstein