Reputation: 502
I have my Django webapp that contains a method in one of my models to upload the user chosen file (from form) to a specified file directory. But I am not able to use the model after I have deployed my app on Google App Engine (using gcloud). I am using the Google MySQL db for my Django app. Also, I am not able to use os.makedir() method in my app as the server prompts there is Read-Only-Permission
MODELS.PY
def upload_to(instance, filename):
now = timezone_now()
base, ext = os.path.splitext(filename)
ext = ext.lower()
rootpath = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
return rootpath + f"/face_detector/static/face_detector/img/{now:%Y%m%d%H%M%S}{ext}"
# Do add the date as it prevents the same file name from occuring twice
# the ext is reserved for the file type and don't remove it
class PicUpClass(models.Model):
picture = models.ImageField(_("picture"), upload_to=upload_to, blank=True, null=True)
ERROR I AM GETTING
OSError at /add-dataset [Errno 30] Read-only file system: '/srv/face_detector/static/face_detector/img/20200228084934.jpg' Request Method: POST Request URL: https://agevent-269406.appspot.com/add-dataset Django Version: 3.0.2 Exception Type: OSError Exception Value: [Errno 30] Read-only file system: '/srv/face_detector/static/face_detector/img/20200228084934.jpg' Exception Location: /env/lib/python3.7/site-packages/django/core/files/storage.py in _save, line 267 Python Executable: /env/bin/python3.7 Python Version: 3.7.6
I am a rookie in python so please suggest me some basic solutions.
Upvotes: 0
Views: 92
Reputation: 2487
The reason you have this Read-only file system error is that the default models.ImageField save uploaded images to your project folder and it's read-only for Google App Engine apps.
You have to use Google Cloud Storage to upload your images (Best Option if it's not temporary data) otherwise for temporary files, you can write them to /tmp directory which will be stored in the instance's RAM but will be deleted if you delete your instance, you scale up or down your instance or if there is no traffic (users) for your instance. If you are using manual scaling, it will not be an issue. But for automatic scaling tmp directory is risky as I stated before.
For the tmp directory:
You can set your rootpath = '/tmp/'
You can retrieve images from the tmp directory by code in views.py to serve them in HTML.
Upvotes: 3