gpt4 book ai didi

python - Kubernetes 集群中的 FileNotFoundError Python-Flask/Docker

转载 作者:行者123 更新时间:2023-12-02 12:12:40 31 4
gpt4 key购买 nike

我在 Flask 应用程序中打开文件时遇到问题,该应用程序在 Kubernetes 集群的 Docker 容器中运行。

每当我尝试打开目录中的文件时,我都会收到 FileNotFoundError。S3FS 对象存储已配置,PersistentVolumeclaim 已成功安装到我的 pod,所以没有问题。

但是,当尝试从挂载的存储中读取文件时,出现上述错误。这是我读取文件的方式。 current_dir/var/www

current_dir = os.path.dirname(__file__)
abs_path = os.path.join(current_dir, path)
with open(abs_path, "rb") as file:
bin_data = file.read()

此外,我还尝试了以下所有变体,但似乎都不起作用:

data_path = pathlib.Path('/var/www/uploads/')
abs_path = data_path / path_id

data_path = pathlib.Path('uploads/')
abs_path = data_path / path_id

abs_path = current_dir + '/uploads' + path_id

tmp = str('/uploads/' + path_id).split('/')
abs_path = os.path.join(os.path.dirname(__file__), *tmp)

我得到的错误如下:

[2020-04-02 18:16:49,359] ERROR in app: Exception on /internal/tusd [POST]
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 2446, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1951, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python3.7/site-packages/flask_cors/extension.py", line 161, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1820, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1949, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1935, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/var/www/api_controller.py", line 95, in tusd_handler
hexdigest = hash_file(str(upload_id), hash_type)
File "/var/www/file_hasher.py", line 17, in hash_file
with open(abs_path, "rb") as file:
FileNotFoundError: [Errno 2] No such file or directory: '/var/www/uploads/f7f1c686fbc40649879e8aee7f529c96'

但是,当我exec -it 进入容器时。该文件等都存在。

$ kubectl exec hasher-savoury-695669b858-nwg5j -- ls /var/www/uploads

f7f1c686fbc40649879e8aee7f529c96
f7f1c686fbc40649879e8aee7f529c96.info

这是我一直在使用的 Dockerfile。注释字段是我为使其正常工作而尝试过的选项。

FROM python:3.7-alpine as base

ENV GROUP_ID=1000 \
USER_ID=1000

RUN apk upgrade && apk update \
&& apk --no-cache add openssl-dev openssl
RUN apk --no-cache --update add --virtual build-dependencies gcc g++ make libffi-dev

RUN mkdir /var/www && mkdir /var/www/uploads
COPY . /var/www
WORKDIR /var/www

RUN pip install --upgrade pip && pip install -r requirements.txt && apk del build-dependencies
RUN pip install gunicorn

#RUN addgroup -g $GROUP_ID savoury
#RUN adduser -D -u $USER_ID -G savoury savoury -s /bin/sh \
# && chown savoury:savoury -R var/www/

#USER savoury

EXPOSE 5080

ENTRYPOINT ["gunicorn"]
CMD ["-w", "4", "--bind", "0.0.0.0:5080", "wsgi"]

我尝试用 Java 读取这些文件并成功读取了它们。我也尝试过“执行”到容器中,并且能够在 python 控制台中轻松执行此操作。我不知道为什么上面的脚本不起作用。

非常感谢任何帮助!

最佳答案

错误背后的原因实际上是找不到文件。它在读取期间不存在,因为与上传服务器的异步事件的时间不正确。由于日志记录不佳,故障未被发现。经验教训:始终记录和监控。

关于python - Kubernetes 集群中的 FileNotFoundError Python-Flask/Docker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60998637/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com