Linux 目录中自动创建的文件的默认权限

发布于 2025-01-10 08:53:41 字数 2576 浏览 0 评论 0原文

我使用 django 框架在 python 中开发的应用程序遇到问题,它使用 FPDF 库导出一个文件,然后应用程序使用该文件将其附加到自动电子邮件中。

当此应用导出 PDF 并将其保存到 media 目录时,该文件不会继承其父目录的权限,而仅具有读/写权限,这不允许 Django 找到该文件这样它就可以附加到邮件中。

我在网上搜索,发现有人有同样的问题,建议他们使用ACL配置来管理默认权限,我尝试了很多不同的方法,但没有成功。我不知道我可能做错了什么(我一直犯同样的错误)。完成 ACL 配置后,文件继续以相同的权限导出,并且当应用命令 chmod 777 -R * 时,这些文件并没有更改其权限,我必须禁用 ACL 配置以使其请允许我应用该命令。

这是出现的错误:

Internal Server Error: /treasury/sendMailsSupplierView/SBOJOZF
Traceback (most recent call last):
  File "/var/www/johannasenvironment/venvjoh/lib/python3.6/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/var/www/johannasenvironment/venvjoh/lib/python3.6/site-packages/django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/var/www/johannasenvironment/JohannasEnviroment/treasuryEmails/views.py", line 33, in sendMailsSupplierView
    sendEmailSupplier('[email protected]', report) #sendEmailSupplier(emailSupplier, report)
  File "/var/www/johannasenvironment/JohannasEnviroment/treasuryEmails/mails/mailsFunctions.py", line 50, in sendEmailSupplier
    email_traslado.attach_file(report)
  File "/var/www/johannasenvironment/venvjoh/lib/python3.6/site-packages/django/core/mail/message.py", line 330, in attach_file
    with path.open('rb') as file:
  File "/usr/lib/python3.6/pathlib.py", line 1183, in open
    opener=self._opener)
  File "/usr/lib/python3.6/pathlib.py", line 1037, in _opener
    return self._accessor.open(self, flags, mode)
  File "/usr/lib/python3.6/pathlib.py", line 387, in wrapped
    return strfunc(str(pathobj), *args)
FileNotFoundError: [Errno 2] No such file or directory: 'media/PaySuppiler--27022022142925.pdf'

这是最新未应用的文件的样子 sudo chmod 777 *

administrador@WEB-APPLICATION:/var/www/johannasenvironment/JohannasEnviroment/media$ ls -l
total 396
-rw-r--r-- 1 www-data www-data 133492 feb 27 09:17 PaySuppiler--27022022141734.pdf
-rw-r--r-- 1 www-data www-data 133492 feb 27 09:28 PaySuppiler--27022022142833.pdf
-rw-r--r-- 1 www-data www-data 133492 feb 27 09:29 PaySuppiler--27022022142925.pdf
administrador@WEB-APPLICATION:/var/www/johannasenvironment/JohannasEnviroment/media$

这些是 media 目录的权限:

drwxrwxrwx  2 administrador administrador  16384 feb 27 09:29 media

I have a problem with an application developed in python using the django framework, it uses the FPDF library to export a file which is then used by the application to attach it to an automated email.

When this app exports the PDF and saves it to the media directory, the file doesn't inherit permissions from its parent directory and only has read/write permissions, this doesn't allow Django to find the file so it can be attached to the mail.

I was searching on the internet and found people with the same problem, they were recommended to use the ACL configuration to manage default permissions, I tried many times with different methods but it did not work. I don't know what I could have done wrong (I kept having the same error). Dfter having made the ACL configuration the files continued to be exported with the same permissions and when applying the command chmod 777 -R * these did not change their permissions, I had to disable the ACL configuration for it to allow me to apply that command.

This is the error that appears:

Internal Server Error: /treasury/sendMailsSupplierView/SBOJOZF
Traceback (most recent call last):
  File "/var/www/johannasenvironment/venvjoh/lib/python3.6/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/var/www/johannasenvironment/venvjoh/lib/python3.6/site-packages/django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/var/www/johannasenvironment/JohannasEnviroment/treasuryEmails/views.py", line 33, in sendMailsSupplierView
    sendEmailSupplier('[email protected]', report) #sendEmailSupplier(emailSupplier, report)
  File "/var/www/johannasenvironment/JohannasEnviroment/treasuryEmails/mails/mailsFunctions.py", line 50, in sendEmailSupplier
    email_traslado.attach_file(report)
  File "/var/www/johannasenvironment/venvjoh/lib/python3.6/site-packages/django/core/mail/message.py", line 330, in attach_file
    with path.open('rb') as file:
  File "/usr/lib/python3.6/pathlib.py", line 1183, in open
    opener=self._opener)
  File "/usr/lib/python3.6/pathlib.py", line 1037, in _opener
    return self._accessor.open(self, flags, mode)
  File "/usr/lib/python3.6/pathlib.py", line 387, in wrapped
    return strfunc(str(pathobj), *args)
FileNotFoundError: [Errno 2] No such file or directory: 'media/PaySuppiler--27022022142925.pdf'

This is what the latest unapplied files look like sudo chmod 777 *:

administrador@WEB-APPLICATION:/var/www/johannasenvironment/JohannasEnviroment/media$ ls -l
total 396
-rw-r--r-- 1 www-data www-data 133492 feb 27 09:17 PaySuppiler--27022022141734.pdf
-rw-r--r-- 1 www-data www-data 133492 feb 27 09:28 PaySuppiler--27022022142833.pdf
-rw-r--r-- 1 www-data www-data 133492 feb 27 09:29 PaySuppiler--27022022142925.pdf
administrador@WEB-APPLICATION:/var/www/johannasenvironment/JohannasEnviroment/media$

These are the permissions of the media directory:

drwxrwxrwx  2 administrador administrador  16384 feb 27 09:29 media

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

顾北清歌寒 2025-01-17 08:53:41

您可以检查一下访问的路径是否正确。根据媒体文件夹内文件的权限,每个用户都有读取权限,并且您正在尝试读取文件(二进制形式)。

此错误(FileNotFoundError:[Errno 2]没有这样的文件或目录:'media/PaySuppiler--27022022142925.pdf')表明路径不正确。尝试使用基于文件的相对路径
例如 os.path.join(os.path.realpath(file), "../media/PaySuppiler--27022022142925.pdf")

为什么我会收到 FileNotFoundError?

Can you check if the path which is being accessed is correct. As per the permissions of the files inside media folder, every user has read permission, and you are trying to read a file (in binary form).

This error (FileNotFoundError: [Errno 2] No such file or directory: 'media/PaySuppiler--27022022142925.pdf') shows that path is not correct. Try using relative path based on the file
e.g os.path.join(os.path.realpath(file), "../media/PaySuppiler--27022022142925.pdf")

Why am I getting a FileNotFoundError?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文