How to use an Azure BlobService object to download a file associated with a Django model

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.

0
source share
1 answer

According to your error message, you missed the parameter in the _save() function, which should be completed in a format such as _save(self,name,content) .

In addition, it seems that you want to put images directly into Azure Storage, which are downloaded from client forms. If so, I found a repo on github that creates its own azure storage class for Django models. We can use it to modify your application. For more information see https://github.com/Rediker-Software/django-azure-storage/blob/master/azure_storage/storage.py

And here are my code snippets, models.py

 from django.db import models from django.conf import settings from django.core.files.storage import Storage from azure.storage.blob import BlobService accountName = 'accountName' accountKey = 'accountKey' class OverwriteStorage(Storage): def __init__(self,option=None): if not option: pass def _save(self,name,content): blob_service = BlobService(account_name=accountName, account_key=accountKey) import mimetypes content.open() content_type = None if hasattr(content.file, 'content_type'): content_type = content.file.content_type else: content_type = mimetypes.guess_type(name)[0] content_str = content.read() blob_service.put_blob( 'mycontainer', name, content_str, x_ms_blob_type='BlockBlob', x_ms_blob_content_type=content_type ) content.close() return name def get_available_name(self,name): return name def upload_path(instance, filename): return 'uploads-from-custom-storage-{}'.format(filename) class Photo(models.Model): image_file = models.ImageField(upload_to=upload_path, storage=OverwriteStorage(), null=True, blank=True ) 
0
source

Source: https://habr.com/ru/post/1238127/


All Articles