Home > Software engineering >  PermissionError when uploading file on new production server
PermissionError when uploading file on new production server

Time:03-20

I migrated my Django project to a new production server. On the previous production server, everything worked fine. While migrating, I upgraded to Ubuntu 20.04 and Django 4.0.3. Now everything is working again, except for the uploading of files.

When I try to create an instance of an Invoice object, it works, as long as I don't try to upload a file along with it. Adding an invoice with a file gives the following errors:

Internal Server Error: /stock/create_invoice
Traceback (most recent call last):
   File "/usr/local/lib/python3.8/dist-packages/django/core/handlers/exception.py", line 55, in inner
     response = get_response(request)
   File "/usr/local/lib/python3.8/dist-packages/django/core/handlers/base.py", line 197, in _get_response
     response = wrapped_callback(request, *callback_args, **callback_kwargs)
   File "/var/www/html/stock/views.py", line 346, in create_invoice
     invoice.save()
   File "/usr/local/lib/python3.8/dist-packages/django/db/models/base.py", line 806, in save
     self.save_base(
   File "/usr/local/lib/python3.8/dist-packages/django/db/models/base.py", line 857, in save_base
     updated = self._save_table(
   File "/usr/local/lib/python3.8/dist-packages/django/db/models/base.py", line 1000, in _save_table
     results = self._do_insert(
   File "/usr/local/lib/python3.8/dist-packages/django/db/models/base.py", line 1041, in _do_insert
     return manager._insert(
   File "/usr/local/lib/python3.8/dist-packages/django/db/models/manager.py", line 85, in manager_method
     return getattr(self.get_queryset(), name)(*args, **kwargs)
   File "/usr/local/lib/python3.8/dist-packages/django/db/models/query.py", line 1434, in _insert
     return query.get_compiler(using=using).execute_sql(returning_fields)
   File "/usr/local/lib/python3.8/dist-packages/django/db/models/sql/compiler.py", line 1620, in execute_sql
     for sql, params in self.as_sql():
   File "/usr/local/lib/python3.8/dist-packages/django/db/models/sql/compiler.py", line 1547, in as_sql
     value_rows = [
   File "/usr/local/lib/python3.8/dist-packages/django/db/models/sql/compiler.py", line 1548, in <listcomp>
     [
   File "/usr/local/lib/python3.8/dist-packages/django/db/models/sql/compiler.py", line 1549, in <listcomp>
     self.prepare_value(field, self.pre_save_val(field, obj))
   File "/usr/local/lib/python3.8/dist-packages/django/db/models/sql/compiler.py", line 1497, in pre_save_val
     return field.pre_save(obj, add=True)
   File "/usr/local/lib/python3.8/dist-packages/django/db/models/fields/files.py", line 316, in pre_save
     file.save(file.name, file.file, save=False)
   File "/usr/local/lib/python3.8/dist-packages/django/db/models/fields/files.py", line 92, in save
     self.name = self.storage.save(name, content, max_length=self.field.max_length)
   File "/usr/local/lib/python3.8/dist-packages/django/core/files/storage.py", line 57, in save
     name = self._save(name, content)
   File "/usr/local/lib/python3.8/dist-packages/django/core/files/storage.py", line 339, in _save
     os.chmod(full_path, self.file_permissions_mode)
 PermissionError: [Errno 1] Operation not permitted: '/var/www/html/media/invoices/veryniceinvoice.pdf'
 Internal Server Error: /stock/invoice/0/
 Traceback (most recent call last):
   File "/usr/local/lib/python3.8/dist-packages/django/core/handlers/exception.py", line 55, in inner
     response = get_response(request)
   File "/usr/local/lib/python3.8/dist-packages/django/core/handlers/base.py", line 197, in _get_response
     response = wrapped_callback(request, *callback_args, **callback_kwargs)
   File "/usr/local/lib/python3.8/dist-packages/django/contrib/auth/decorators.py", line 23, in _wrapped_view
     return view_func(request, *args, **kwargs)
   File "/var/www/html/stock/views.py", line 159, in invoice
     invoice = Invoice.objects.get(pk=invoice_id)
   File "/usr/local/lib/python3.8/dist-packages/django/db/models/manager.py", line 85, in manager_method
     return getattr(self.get_queryset(), name)(*args, **kwargs)
   File "/usr/local/lib/python3.8/dist-packages/django/db/models/query.py", line 496, in get
     raise self.model.DoesNotExist(
      stock.models.Invoice.DoesNotExist: Invoice matching query does not exist.

The file is uploaded to the correct (cifs mounted) folder, but for some reason, an instance of an object containing the link to the file cannot be added to the database?

I suppose the problem is some kind of permission issue. I tried solving this by setting everything to 777, so I could at least see whether that solves the issue, but it does not. The folder permissions of /var/www/html/media are currently:

drwxrwxrwx 2 root root 0 Mar 17 08:57 invoices

chown doesn't work for some reason (I read about this having something to do with the cifs mount?), but var-www wasn't the owner of the invoices folder on the previous server either, and that worked fine...

Any help would be very much appreciated!

CodePudding user response:

I found the solution.

I had to add uid=www-data,forceuid to the cifs mount command. The apache user did not have the correct permissions.

(Then unmount, and mount again)

  • Related