/
azure_storage.py
60 lines (40 loc) · 2.23 KB
/
azure_storage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# pylint: disable=abstract-method
# Disable: Method 'path' is abstract in class 'Storage' but is not overridden
"""Django storage classes to use with Azure Blob storage service."""
from azure.common import AzureMissingResourceHttpError
from django.conf import settings
from django.contrib.staticfiles.storage import ManifestFilesMixin
from storages.backends.azure_storage import AzureStorage
from readthedocs.builds.storage import BuildMediaStorageMixin
from .mixins import OverrideHostnameMixin
class AzureBuildMediaStorage(BuildMediaStorageMixin, OverrideHostnameMixin, AzureStorage):
"""An Azure Storage backend for build artifacts."""
azure_container = getattr(settings, 'AZURE_MEDIA_STORAGE_CONTAINER', None) or 'media'
override_hostname = getattr(settings, 'AZURE_MEDIA_STORAGE_HOSTNAME', None)
def url(self, name, expire=None, http_method=None): # noqa
"""
Override to accept ``http_method`` and ignore it.
This method helps us to bring compatibility between Azure Blob Storage
(which does not use the HTTP method) and Amazon S3 (who requires HTTP
method to build the signed URL).
"""
return super().url(name, expire)
class AzureBuildStorage(AzureStorage):
"""An Azure Storage backend for build cold storage."""
azure_container = getattr(settings, 'AZURE_BUILD_STORAGE_CONTAINER', None) or 'builds'
class AzureStaticStorage(OverrideHostnameMixin, ManifestFilesMixin, AzureStorage):
"""
An Azure Storage backend for static media.
* Uses Django's ManifestFilesMixin to have unique file paths (eg. core.a6f5e2c.css)
"""
azure_container = getattr(settings, 'AZURE_STATIC_STORAGE_CONTAINER', None) or 'static'
override_hostname = getattr(settings, 'AZURE_STATIC_STORAGE_HOSTNAME', None)
def read_manifest(self):
"""Handle a workaround to make Azure work with Django on the first 'collectstatic'."""
try:
return super().read_manifest()
except AzureMissingResourceHttpError:
# Normally Django handles this transparently as long as failing
# to read the manifest throws an IOError. However, failing to
# read a missing file from Azure storage doesn't currently
return None