请求中的Gunicorn/Flask文件为0.0 kb

发布于 2025-01-26 12:03:29 字数 1374 浏览 0 评论 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)

上面是我用来接收由用户上传的PDF文件的示例代码。此PDF由AWS Lambda和其他内部服务发送。我正在使用Python-Requests发送这些请求。

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

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

这些天我经常面临的问题是,我保存的文件是0.0kb,即。 file_size = 0.0 kb

我正在使用nginx-ingress,并且该应用在GCP的Kubernetes中部署。

我发现的一些常见原因是服务器中缺乏足够的存储空间,我已经检查了这一点,这对我而言并非如此。这里还有其他问题?

如果我缺少有关此的其他上下文,请告诉我。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文