Home > Blockchain >  How to upload a Python Requests content to S3 without saving the file
How to upload a Python Requests content to S3 without saving the file

Time:04-23

I am running a Python Lambda Function that downloads an MP3 file using the Requests library from a server and then uploads the file to S3. This is the code I am using which is working fine:

dl_url = "https://server.com/file.mp3"
response = requests.get(dl_url)
s3 = boto3.resource('s3')
bucket = s3.Bucket('my-bucket')
file_name = "file.mp3"
dir_file = f"/tmp/{file_name}"

with open(dir_file, "wb") as f:
    f.write(response.content)

bucket.upload_file(dir_file, file_name)

This code works fine, however I was wondering if I can skip the step of saving the file first and then uploading the file.

CodePudding user response:

Working code that doesn't require you to save the file first:

import boto3
import requests

s3_client = boto3.client('s3')

bucket = "test-bucket"

dl_url = "https://file-examples.com/storage/fe1170c1cf625dc95987de5/2017/11/file_example_MP3_700KB.mp3"
response = requests.get(dl_url)

# Write the object
s3_client.put_object(Bucket=bucket,
                     Key="sample_mp3.mp3",
                     Body=response.content)
  • Related