From the course: Advanced Python Projects: Build AI Applications

Unlock the full course today

Join today to access over 24,200 courses taught by industry experts.

Upload files to Amazon S3 storage bucket

Upload files to Amazon S3 storage bucket - Python Tutorial

From the course: Advanced Python Projects: Build AI Applications

Upload files to Amazon S3 storage bucket

- [Instructor] Next we're going to learn how to upload a file to Amazon S3 storage. So we're going to have the /uploadFile FastAPI endpoint asynchronously process the file uploads. We're going to have it temporarily saved the file locally, upload it to Amazon S3, remove it from the local file system, and return a JSON response containing the file name and S3 file path, with the appropriate exception handling at the end. Let's check out how to do that. So over here in this section, we defined a FastAPI endpoint at /uploadFile that handles the HTTP post request. The function upload to S3 is an asynchronous function that takes a file input using the FileUpload type. Then we print the file name extracted from the uploaded file path. Next, we're going to be uploading our files to the S3. This code opens the local file for writing in binary mode, reads the content of the uploaded file, asynchronously writes the content to the…

Contents