简体   繁体   中英

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

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM