请求中的Gunicorn/Flask文件为0.0 kb
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论