[英]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. 如何将Azure blob存储链接到Django管理员, 以便在保存新记录时将文件上传到 blob存储帐户。
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. 尽管我知道图像文件实际上并未存储在我的SQLite3数据库中,但管理界面的行为就像在单击保存之前附加了图像。
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. 当将图像手动上传到Azure blob存储帐户时,我可以在项目的面向消费者的部分中成功引用它们。 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 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 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 使用Django 1.7,Python 2.7,SQLite3
Django-storages has support for an Azure blob backend which would allow any uploads you do to be automatically stored in your storage container. Django-storages支持Azure blob后端,这将使您所做的任何上载都自动存储在存储容器中。
http://django-storages.readthedocs.org/en/latest/backends/azure.html http://django-storages.readthedocs.org/en/latest/backends/azure.html
My Configuration 我的配置
# Media
MEDIA_ROOT = '/home/<user>/media/'
MEDIA_URL = '/media/'
Remember folder need permission (write, read) apache user example: 记住文件夹需要权限(写,读)的apache用户示例:
<img src="/media/img/my_image.png">
or 要么
<img src="{{obj.file.url}}">
I'm not aware of any built-in Django API that allows us to change the blob's content type. 我不知道任何允许我们更改Blob内容类型的内置Django API。 But from my experience, you can use Azure SDK for Python to upload blobs: https://github.com/Azure/azure-sdk-for-python . 但是根据我的经验,您可以使用适用于Python的Azure SDK来上传blob: 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. 默认情况下,内容类型为application / octet-stream。 However you can change it via x_ms_blob_content_type. 但是,您可以通过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. 请参考https://azure.microsoft.com/en-us/documentation/articles/storage-python-how-to-use-blob-storage/以获取示例,如果您还有其他疑问 ,请随时告诉我们。关注。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.