0
import os
import tempfile
from flask import Flask, request
from werkzeug.utils import secure_filename

import uuid
app = Flask(__name__)

@app.route("/upload/", methods=["POST"])
def save_file():
    files = request.files
    files = [(j.filename, j) for _, j in files.items()]
    print(files)
    for filename, body in files:
        tmp_dir = tempfile.TemporaryDirectory()
        filename = os.path.join(tmp_dir.name, secure_filename(filename))
        body.save(filename)
        file_size = 0
        if os.path.exists(filename):
            file_size = round(os.path.getsize(filename) / 1024, 2)
            print(f"File size {file_size}")

        if file_size == 0:
            return {"msg": "File size is 0"}, 400
    
    return {}, 200

if __name__ == "__main__":
    app.run(debug=True)

Above is a sample code that I am using to receive a pdf file that has been uploaded by the user. This pdf is sent by either aws lambda and other internal services. I am using python-requests to send these request.

multipart_form_data ={'files': (filename_safe, open(filename, 'rb'))}

result = requests.post("xyz.com/upload/", 
             files=multipart_form_data,
             headers={'token': token}
         )

The issue I am facing quite often these days is that the file that I am saving comes out to be 0.0KB ie. file_size = 0.0 KB

I am using nginx-ingress and the app is deployed in kubernetes in gcp.

Some of the common reasons I found is lack of enough storage in the server, I've check that and it's not the case for me. What could be some other problems here?

If i am missing additional context around this, please let me know.

theredcap
  • 632
  • 6
  • 13
  • This code will also report size = 0 if the file does not exist. How sure are you that the file really does exist under that name? – Thomas May 04 '22 at 11:58
  • You might want to check info about file first: https://stackoverflow.com/a/41546847/2119941 – Hrvoje May 04 '22 at 12:03

0 Answers0