EC2 with Docker and Flask API File Upload

0

hello everyone, i have a docker instance running on an aws ec2 instance. on the docker container i have a flask server running. Now i am trying to upload a image file via post from a local python file. But somehow the file gets corrupted during upload. i am not able to open the image with Pillow. on my local machine the file is correct. if i do the same setup with the same code and docker container on my local instance everything works.

file testFolder/input.png testFolder/input.png: PNG image data, 1024 x 1024, 8-bit/color RGB, non-interlaced but on the server its broken

file input.png input.png: data

my client code:

# client.py

import requests

url = ''
apiKey = ""
headers = {'x-api-key': apiKey}
files = {'file': open('input.png', 'rb')}

response = requests.post(url, files=files, headers=headers)

print(response.text)

my server code:

# server.py

from flask import Flask, request, jsonify
import os

app = Flask(__name__)

@app.route('/upload', methods=['POST'])
def upload_file():
    if 'file' not in request.files:
        return jsonify(message="No file part"), 400

    file = request.files['file']

    if file.filename == '':
        return jsonify(message="No selected file"), 400

    if file and file.filename.endswith('.png'):
        file_path = os.path.join('uploads', file.filename)
        file.save(file_path)
        return jsonify(message="File uploaded successfully", file_path=file_path), 200
    else:
        return jsonify(message="Invalid file format, only ZIP allowed"), 400

if __name__ == '__main__':
    if not os.path.exists('uploads'):
        os.makedirs('uploads')

    app.run(host='0.0.0.0', port=80, debug=True)

if i copy the file via scp the file is correct. the normal stuff like sending a request and reciving an answer is working correctly. only the file upload is broken somehow. is there any specific configuration to enable file upload to the ec2 instance with the running docker container. (security groups, ACLs ...) thank you

Paul
asked 4 months ago235 views
1 Answer
0

Updated response: Investigate the following areas:

  • Ensure that your Docker container has the necessary permissions and configurations to handle file I/O operations correctly.
  • Corrupted files can sometimes be a result of issues during transmission over the network. You could add some logging in your Flask app to check the file size before and after the upload.
  • Try uploading the file using cURL or Postman. If it works with these tools but not with your Python client, the issue might be in the client code.

Additional suggestions to try out based on the conversation we have by email, Based on the code and the issue you described (file size increasing after being received on the server), let's go through some specific aspects to check:

  • Instead of using request.content_length which includes the entire request payload, log the actual size of the file object (file) after it's received but before saving it.
  • In your client code, verify the Content-Type is set to multipart/form-data by logging the request headers after sending the file.
  • On the server, inspect and log the first and last few bytes of the saved file to check for any additional data appended.
  • Upload different file types (like text files) to see if the issue is specific to PNGs or binary files in general.
  • Compare your local and server environments, focusing on Python, Flask, and requests library versions.
  • Ensure your Flask and its underlying Werkzeug library are up-to-date on the server.

Let me know if you have any further questions to discuss.

Best regards! Mina


edit: removed email address: Zack M

profile picture
EXPERT
answered 4 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions