创建具有正确权限的文件夹

时间:2022-10-05 20:58:38

When uploading files, I am setting the upload_to to a function that creates a path doesn't exist, and Django is creating the extra folders (with permissions I don't like).

上传文件时,我将upload_to设置为一个创建路径不存在的函数,Django正在创建额外的文件夹(具有我不喜欢的权限)。

For example,

例如,

def photo_upload_path(instance, filename):  
    return os.path.join('photos', str(instance.id), filename)

however, the permissions of the folder are not what I would like. Should I create the folder and set the permissions? Or is there a setting for this?

但是,文件夹的权限不是我想要的。我应该创建文件夹并设置权限吗?或者有这样的背景吗?

I found a post for controlling permissions of uploaded files (django / file uploads permissions), but not created directories.

我找到了一个控制上传文件权限的帖子(django / file uploadpermissions),但没有创建目录。

Thanks for any help!

感谢任何帮助!

2 个解决方案

#1


2  

You could change the default directory permission in your settings file. This is available after Django 1.7, and is fully described in the Django documentation. Note that this will affect all directories created by Django, including staticfiles, so use with caution.

您可以更改设置文件中的默认目录权限。这可以在Django 1.7之后获得,在Django文档中有完整的描述。注意,这会影响Django创建的所有目录,包括staticfiles,所以要小心使用。

FILE_UPLOAD_DIRECTORY_PERMISSIONS = 0o755

Change the example code to use the permission you desire. I didn't use your requested permission (0o777) in my example code in case someone copies without reading.

更改示例代码以使用所需的权限。在我的示例代码中,我没有使用您的请求权限(0o777),以防有人在没有阅读的情况下复制。

#2


0  

so I ended up finding out a solution for the permissions.

所以我最终找到了一个许可的解决方案。

I was using apache, so I believe it was actually not 100% a django issue.

我使用的是apache,所以我认为这并不是一个100%的django问题。

If I edit /etc/apache2/envvars and set add a line such as umask u=rwx,g=rwx,o=rwx, it sets the default permissions

如果我编辑/etc/apache2/ envars并设置添加一行,如umask u=rwx、g=rwx、o=rwx,则设置默认权限

#1


2  

You could change the default directory permission in your settings file. This is available after Django 1.7, and is fully described in the Django documentation. Note that this will affect all directories created by Django, including staticfiles, so use with caution.

您可以更改设置文件中的默认目录权限。这可以在Django 1.7之后获得,在Django文档中有完整的描述。注意,这会影响Django创建的所有目录,包括staticfiles,所以要小心使用。

FILE_UPLOAD_DIRECTORY_PERMISSIONS = 0o755

Change the example code to use the permission you desire. I didn't use your requested permission (0o777) in my example code in case someone copies without reading.

更改示例代码以使用所需的权限。在我的示例代码中,我没有使用您的请求权限(0o777),以防有人在没有阅读的情况下复制。

#2


0  

so I ended up finding out a solution for the permissions.

所以我最终找到了一个许可的解决方案。

I was using apache, so I believe it was actually not 100% a django issue.

我使用的是apache,所以我认为这并不是一个100%的django问题。

If I edit /etc/apache2/envvars and set add a line such as umask u=rwx,g=rwx,o=rwx, it sets the default permissions

如果我编辑/etc/apache2/ envars并设置添加一行,如umask u=rwx、g=rwx、o=rwx,则设置默认权限