Download media with Lambda, then upload to S3

30 Views Asked by At

I have a Lambda function that needs to download a file from a 3th party server, and then upload it to my S3 bucket. The problem is when I manually download the file from the bucket, it seems the file is corrupted.

with httpx.stream("GET", get_media_url["url"], headers=META_API_HEADERS) as get_media_file:
    with tempfile.NamedTemporaryFile(delete=False, suffix=".ogg") as temp_file:
        for chunk in get_media_file.iter_raw():
            temp_file.write(chunk)
        temp_file_path = temp_file.name
        s3.Bucket(MY_BUCKET).upload_file(temp_file_path, f"{sender}/{media_id}.ogg")

Looking at the Object overview tab in S3, I can see the file has a decent size, and file type is ogg as I expected. I'm getting this media file from Meta (Whatsapp Business Cloud API), so I doubt the problem is with their side. Also, I see a 30% error margin - that means that not all files show up as corrupted. I also added a decent timeout to Lambda so the function won't timeout in the middle of the download.

Any help or insight would be appreciated.

1

There are 1 best solutions below

0
Croves On

As per @Artem Arkhipov comment, I indeed had to set the MIME type for the file, but I also had to call the upload function outside the NamedTemporaryFile context manager.

Thank you!