I have a Django app where users upload photos and descriptions. Here's a typical model that facilitates user behavior:
class Photo(models.Model): description = models.TextField(validators=[MaxLengthValidator(500)]) submitted_on = models.DateTimeField(auto_now_add=True) image_file = models.ImageField(upload_to=upload_to_location, null=True, blank=True )
Note that the image_file attribute has an upload_to argument, which loads the upload directory and image_file file name . The upload_to_location method upload_to_location take care of this; Suppose it works correctly.
Now I want to upload each image to Azure Cloud Storage. Below is a python snippet for this. Using this, I tried to write my own repository that stores images in Azure. However, this is a buggy, and I need help cleaning it up. Here is what I did:
Changed the image_file attribute in models.py :
image_file = models.ImageField("Tasveer dalo:",upload_to=upload_to_location, storage=OverwriteStorage(), null=True, blank=True )
And then create a separate storage.py in my application folder, which has:
from django.conf import settings from django.core.files.storage import Storage from azure.storage.blob import BlobService class OverwriteStorage(Storage): def __init__(self,option=None): if not option: pass def _save(name,content): blob_service = BlobService(account_name='accname', account_key='key') PROJECT_ROOT = path.dirname(path.abspath(path.dirname(__file__))) try: blob_service.put_block_blob_from_path( 'containername', name, path.join(path.join(PROJECT_ROOT,'uploads'),name), x_ms_blob_content_type='image/jpg' ) return name except: print(sys.exc_info()[1]) return 0 def get_available_name(self,name): return name
This setting does not work and returns an error: _save() takes exactly 2 arguments (3 given). Exception Location: /home/hassan/.virtualenvs/redditpk/local/lib/python2.7/site-packages/django/core/files/storage.py in save, line 48 _save() takes exactly 2 arguments (3 given). Exception Location: /home/hassan/.virtualenvs/redditpk/local/lib/python2.7/site-packages/django/core/files/storage.py in save, line 48
How do I do this job? Has anyone used the Azure-Storage python SDK with their Django projects this way? Please inform.
Note. Initially, I used the django-storages , which tricked the storage information from me, reducing everything to some configuration, which will be entered in settings.py. But now I need to remove django-storages from the equation and use this Azure-Storage python SDK for this purpose.
Note. Request more information if you need it.