How to Read the Size of an Image Before Uploading it to Amazon S3 Using Python Flask

How to Read the Size of an Image Before Uploading it to Amazon S3 Using Python Flask
Python Flask is a powerful microframework that allows developers to build web applications. There are situations where we need to read the size of an image before uploading it to Amazon S3. In this blog post, we’ll guide you through the process.
Introduction
When dealing with file uploads, it is crucial to validate the file size before uploading it to your storage, in this case, Amazon S3. This helps in avoiding unnecessary costs and ensuring efficient usage of resources.
Prerequisites
To follow this guide, you’ll need:
- Python installed on your machine
- Basic understanding of Flask
- An Amazon Web Services (AWS) account
The Flask Side of Things
First, let’s look at how to get the file size from the Flask side. Flask has a request
object that allows us to access the file size. Here’s how you can do it:
from flask import Flask, request
app = Flask(__name__)
@app.route('/upload', methods=['POST'])
def upload_file():
file = request.files['file']
file_size = len(file.read())
return f"File size is {file_size} bytes"
In this code snippet, we first import the necessary modules. We then create a Flask application instance. The @app.route
decorator is used to bind a function to a URL route, in this case, ‘/upload’. When we get a POST request on this route, the upload_file
function is called.
Inside the function, we access the uploaded file with request.files['file']
. To get the file size, we read the file and then use the len
function. The size is returned in bytes.
Uploading to Amazon S3
Now that we have the file size, let’s see how we can upload the file to Amazon S3 using the Boto3 library:
import boto3
def upload_file_to_s3(file, bucket_name, file_name):
s3 = boto3.client('s3')
try:
s3.upload_fileobj(file, bucket_name, file_name)
return "File uploaded successfully"
except Exception as e:
return(str(e))
In this function, we first create an S3 client. We then use the upload_fileobj
method of the S3 client to upload the file to the specified bucket bucket_name
with the specified file name file_name
. If the upload is successful, we return a success message. If there’s an exception, we catch it and return the exception message.
Connecting the Dots
Let’s combine these two parts and create a full Flask application:
from flask import Flask, request
import boto3
app = Flask(__name__)
@app.route('/upload', methods=['POST'])
def upload_file():
file = request.files['file']
file_size = len(file.read())
if file_size < 10485760: # Limit file size to 10MB
response = upload_file_to_s3(file, 'mybucket', file.filename)
return response
else:
return "File size exceeds limit"
def upload_file_to_s3(file, bucket_name, file_name):
s3 = boto3.client('s3')
try:
s3.upload_fileobj(file, bucket_name, file_name)
return "File uploaded successfully"
except Exception as e:
return(str(e))
if __name__ == '__main__':
app.run(debug=True)
In this final version, we added a condition to check if the file size exceeds 10MB. If it does, we return an error message. Otherwise, we upload the file to Amazon S3 and return the response of the upload function.
Conclusion
This guide explained how to read the size of an image before uploading it to Amazon S3 using Python Flask. This is a good practice to control the size of the files being uploaded to your storage, leading to better usage of resources and cost savings. Happy coding!
About Saturn Cloud
Saturn Cloud is your all-in-one solution for data science & ML development, deployment, and data pipelines in the cloud. Spin up a notebook with 4TB of RAM, add a GPU, connect to a distributed cluster of workers, and more. Join today and get 150 hours of free compute per month.