3

When I upload a file into Django server,the file permission seems to be like

-rw-r--r-- 1 bangtest nobody 5198 May  2 00:37 image.jpg

But I want to upload these files as root user,how can I do that using python in Django application.

I have tried using this

import pwd
import grp
import os

uid = pwd.getpwnam("root").pw_uid
gid = grp.getgrnam("root").gr_gid
path = '/home/bangtest/alpha/media/products/image_2081.jpg'
os.chown(path, uid, gid)

Then I am getting error like

OSError: [Errno 1] Operation not permitted: '/home/bangtest/alpha/media/products/image_2081.jpg'

Suggest me a solution for this issue.

Thanks

Community
  • 1
  • 1
Ramakrishna
  • 686
  • 1
  • 12
  • 25
  • Why would you change it to the root user? – François Constant May 02 '14 at 05:57
  • Hello @François,Erlier I have faced a problem while accessing saved images from mod_wsgi apache,this [link](http://stackoverflow.com/questions/23320821/django-media-url-not-found) is my problem.After changing the file owner from bangtest to root I am able to access the files.Because of that reason only I want to change it to the root user. – Ramakrishna May 02 '14 at 06:03
  • 1
    What is the process running as root that wants to access them? It wouldn't be Apache as when it is handling requests it is running as a different user and would never run as root. Any other process running as root should be able to access the files regardless of permissions. – Graham Dumpleton May 02 '14 at 06:34

1 Answers1

1

You have some options, I always try these two:

  1. Easy way: Set permissions to 775 in the folder you need to interact, or 777, if you need to save information (insecure).
  2. Hard way: Add www/nobody or your apache's user name to your user group, and make the folder writable and readable for that group users (or simply add permissions for www or your apache/nginx user in that folder).
k4rtik
  • 259
  • 1
  • 5
  • 18
AlvaroAV
  • 10,335
  • 12
  • 60
  • 91