使用Azure存储SDK使用Django(并且完全取消对Django的存储器依​​赖) [英] Using Azure Storage SDK with Django (and removing dependency on django-storages entirely)

查看:125
本文介绍了使用Azure存储SDK使用Django(并且完全取消对Django的存储器依​​赖)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人有使用 Azure的存储直接与Django的?我问,因为我目前正在建立的Azure云存储为我的Django应用程序(托管在Azure虚拟机与Ubuntu OS)和 Django的货仓似乎并不与Azure存储SDK接口正确(已知问题:<一href=\"http://stackoverflow.com/questions/33151766/using-azure-as-a-storage-backend-for-django-using-django-storages\">see这里)。此修复程序上市给我的Django的版本对我来说是行不通为&lt; 1.6.2。

Does anyone have any tips on using azure-storage directly with Django? I ask because currently I'm trying to set up Azure Cloud Storage for my Django app (hosted on Azure VM with Ubuntu OS), and django-storages doesn't seem to be interfacing with Azure Storage SDK correctly (known issue: see here). The fix listed there won't work for me given my Django version is < 1.6.2.

因此​​我需要直接与Django中使用Azure的存储。有没有人设置?我需要保存图片的MP3 的云存储。

Thus I'll need to use Azure-storage directly with Django. Has anyone set that up before? I need to save images and mp3s on the Cloud Storage.

目前,在我的models.py,我有:

Currently, in my models.py, I have:

def upload_to_location(instance, filename):
    try:
        blocks = filename.split('.') 
        ext = blocks[-1]
        filename = "%s.%s" % (uuid.uuid4(), ext)
        instance.title = blocks[0]
        return os.path.join('uploads/', filename)
    except Exception as e:
        print '%s (%s)' % (e.message, type(e))
        return 0

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 )

然后 Django的货仓博托拿剩下的事情。然而,当我勾Django的存储器与Azure的云存储,我得到以下错误:

And then django-storages and boto take care of the rest. However, when I hook django-storages up with Azure Cloud Storage, I get the following error:

Exception Value:      
'module' object has no attribute 'WindowsAzureMissingResourceError'

Exception Location:     
/home/mhb11/.virtualenvs/myvirtualenv/local/lib/python2.7/site-packages/storages/backends/azure_storage.py in exists, line 46

而code的相关片段是:

And the relevant snippet of the code is:

def exists(self, name):
    try:
        self.connection.get_blob_properties(
            self.azure_container, name)
    except azure.WindowsAzureMissingResourceError:
        return False
    else:
        return True

似乎对Azure的容器连接失败。在我的settings.py,我有:

Seems that the connection to the Azure container is failing. In my settings.py, I have:

    DEFAULT_FILE_STORAGE = 'storages.backends.azure_storage.AzureStorage'
    AZURE_ACCOUNT_NAME = 'photodatabasestorage'
    AZURE_ACCOUNT_KEY = 'something'
    AZURE_CONTAINER = 'somecontainer'

如前所述,我需要完全绕过Django的存储器解决方案,仅仅依靠Azure存储SDK来完成这项工作。

As described earlier, I need a solution that bypasses django-storages entirely, and just relies on Azure Storage SDK to get the job done.

注:向我要了解更多信息的情况下,你需要它

推荐答案

我们可以直接使用 Azure-在Django的存储蟒蛇SDK应用就像普通的Python应用程序使用SDK。你可以参考<一个href=\"https://azure.microsoft.com/en-us/documentation/articles/storage-python-how-to-use-blob-storage/\"相对=nofollow>入门官方指南。

We can directly use Azure-Storage python SDK in the Django apps just like using the sdk in common python applications. You can refer to official guide for getting started.

下面是在Django应用程序测试code片断:

Here is the test code snippet in the Django app:

def putfiles(request):
blob_service = BlobService(account_name=accountName, account_key=accountKey)
PROJECT_ROOT = path.dirname(path.abspath(path.dirname(__file__)))
try:
    blob_service.put_block_blob_from_path(
            'mycontainer',
            '123.jpg',
            path.join(path.join(PROJECT_ROOT,'uploads'),'123.jpg'),
            x_ms_blob_content_type='image/jpg'
    )
    result = True
except:
    print(sys.exc_info()[1])
    result = False
return HttpResponse(result)


def listfiles(request):
    blob_service = BlobService(account_name=accountName, account_key=accountKey)
    blobs = []
    result = []
    marker = None
    while True:
        batch = blob_service.list_blobs('mycontainer', marker=marker)
        blobs.extend(batch)
        if not batch.next_marker:
            break
        marker = batch.next_marker
    for blob in blobs:
        result.extend([{'name':blob.name}])
    return HttpResponse(json.dumps(result))

这篇关于使用Azure存储SDK使用Django(并且完全取消对Django的存储器依​​赖)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆