Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用Azure';s BlobService对象以上载与Django模型关联的文件_Python_Django_Azure_Azure Cloud Services - Fatal编程技术网

Python 如何使用Azure';s BlobService对象以上载与Django模型关联的文件

Python 如何使用Azure';s BlobService对象以上载与Django模型关联的文件,python,django,azure,azure-cloud-services,Python,Django,Azure,Azure Cloud Services,我有一个Django应用程序,用户可以上传照片和描述。下面是一个典型的模型,它促进了用户行为: class Photo(models.Model): description = models.TextField(validators=[MaxLengthValidator(500)]) submitted_on = models.DateTimeField(auto_now_add=True) image_file = models.ImageField(upload_t

我有一个Django应用程序,用户可以上传照片和描述。下面是一个典型的模型,它促进了用户行为:

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 )
请注意,image\u文件属性具有
upload\u to
参数,该参数提供了image\u文件的上载目录和文件名
upload\u-to\u-location
方法解决了这个问题;假设它工作正常

现在我想将每个图像上传到Azure云存储。要做到这一点的python代码段是。使用它,我尝试编写自己的自定义存储,将图像保存到Azure。不过它有四轮马车,我需要帮忙清理。以下是我所做的:

models.py中的
image\u文件
属性更改为:

image\u file=models.ImageField(“Tasveer dalo:”,upload\u to=upload\u to\u location,storage=overwritestrage(),null=True,blank=True)

然后在我的应用程序文件夹中创建一个单独的storage.py,该文件夹具有:

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
此设置不起作用,并返回错误:
\u save()只接受2个参数(给定3个)。异常位置:/home/hassan/.virtualenvs/redditpk/local/lib/python2.7/site-packages/django/core/files/storage.py保存,第48行

我该怎么做?有没有人以这种方式在Django项目中使用Azure存储python SDK?请告知

注意:最初,我使用的是
django-storages
库,它模糊了我的存储详细信息,将所有内容简化为要在settings.py中输入的一些配置。但是现在,我需要从等式中删除
django存储
,并将其单独用于此目的


注意:如果需要,请询问更多信息。

根据您的错误消息,您在函数
\u save()
中遗漏了参数,该参数应以
\u save(self,name,content)
等格式填写

此外,您似乎希望将图像直接放到Azure存储中,这些图像是从客户端表单上载的。如果是这样,我在github中找到了一个repo,它为Django模型构建了一个定制的azure存储类。我们可以利用它来修改您的应用程序。有关详细信息,请参阅

这是我的代码片段, 型号.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 )

加里,当我尝试这个的时候,有些奇怪的事情发生了。我的代码顺利通过
\u save
,一直到
返回name
content.closed()后面的行)。但它会在之后的某个地方弹出一个错误,给我
NotImplementedError at/没有提供异常。异常位置:url第121行的home/hassan/.virtualenvs/redditpk/local/lib/python2.7/site-packages/django/core/files/storage.py。Azure blob中确实出现了一个对象,但它没有加载,并且出现了错误。内容类型会有问题吗?如何调试?我意识到这个问题与上传文件是分开的-它与在Django中设置imagefield属性的url有关。我已将其设置为不同的SO帖子: