Julia Tamsma
Julia Tamsma

Reputation: 15

Django not uploading image files to Azure storage container

How do I link my Azure blob storage to my Django admin, such that it uploads the file to the blob storage account when saving a new record.

I have my image upload set up in the admin already. The admin interface acts like the image is attached before I click save, although I am aware that the image file is not actually stored in my SQLite3 database.

I can reference them successfully in the consumer-facing portion of my project when the images are manually uploaded to the Azure blob storage account. I don't want to manually upload them each time, for obvious reasons.

There has to be a simple solution for this, I just haven't had success in researching it. Thanks in advance!

models.py

class Image(models.Model):
    file = models.ImageField(upload_to='img/')

    def __unicode__(self):
        return u'%s' % self.file 

class Product(models.Model):
...
    picture = models.ManyToManyField(Image)
...

settings.py

MEDIA_ROOT = path.join(PROJECT_ROOT, 'media').replace('\\', '/')

MEDIA_URL = 'https://my_site.blob.core.windows.net/'

Using Django 1.7, Python 2.7, SQLite3

Upvotes: 1

Views: 2847

Answers (3)

Ming Xu - MSFT
Ming Xu - MSFT

Reputation: 2116

I'm not aware of any built-in Django API that allows us to change the blob's content type. But from my experience, you can use Azure SDK for Python to upload blobs: https://github.com/Azure/azure-sdk-for-python. The most important setting in your case is the content type. By default content type is application/octet-stream. However you can change it via x_ms_blob_content_type. Please refer to https://azure.microsoft.com/en-us/documentation/articles/storage-python-how-to-use-blob-storage/ for a sample and feel free to let us know if you have any further concerns.

Upvotes: 0

erajuan
erajuan

Reputation: 2294

My Configuration

# Media
MEDIA_ROOT = '/home/<user>/media/'
MEDIA_URL = '/media/'

Remember folder need permission (write, read) apache user example:

<img src="/media/img/my_image.png">

or

<img src="{{obj.file.url}}">

Upvotes: -1

theadriangreen
theadriangreen

Reputation: 2258

Django-storages has support for an Azure blob backend which would allow any uploads you do to be automatically stored in your storage container.

http://django-storages.readthedocs.org/en/latest/backends/azure.html

Upvotes: 2

Related Questions