Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Azure private storage #3797

Merged
merged 2 commits into from
May 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions kobo/apps/storage_backends/private_azure_storage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from django.urls import reverse
from django.utils import timezone
from django.utils.deconstruct import deconstructible
from private_storage import appconfig
from storages.backends.azure_storage import AzureStorage
from storages.utils import setting


@deconstructible
class PrivateAzureStorage(AzureStorage):
"""
Ported from PrivateS3BotoStorage
"""
def url(self, name, *args, **kwargs):
# S3_REVERSE_PROXY because Azure doesn't exist in private_storage.appconfig
if appconfig.PRIVATE_STORAGE_S3_REVERSE_PROXY or not self.querystring_auth:
# There is no direct URL possible, return our streaming view instead.
return reverse('serve_private_file', kwargs={'path': name})
else:
# The S3Boto3Storage can generate a presigned URL that is temporary available.
return super().url(name, *args, **kwargs)

def get_modified_time(self, name):
# workaround https://github.com/jschneier/django-storages/issues/1131
# If fixed upstream, delete this function
properties = self.client.get_blob_client(
self._get_valid_path(name)
).get_blob_properties(
timeout=self.timeout
)
if not setting('USE_TZ', False):
return timezone.make_naive(properties.last_modified)

tz = timezone.get_current_timezone()
if timezone.is_naive(properties.last_modified):
return timezone.make_aware(properties.last_modified, tz)

# `last_modified` is in UTC time_zone, we
# must convert it to settings time_zone
return properties.last_modified.astimezone(tz)
6 changes: 5 additions & 1 deletion kobo/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -659,11 +659,15 @@ def __init__(self, *args, **kwargs):
# Proxy S3 through our application instead of redirecting to bucket
# URLs with query parameter authentication
PRIVATE_STORAGE_S3_REVERSE_PROXY = True
if 'AZURE_ACCOUNT_NAME' in os.environ:
if DEFAULT_FILE_STORAGE.endswith("AzureStorage"):
PRIVATE_STORAGE_CLASS = \
'kobo.apps.storage_backends.private_azure_storage.PrivateAzureStorage'
PRIVATE_STORAGE_S3_REVERSE_PROXY = True # Yes S3
AZURE_ACCOUNT_NAME = env.str('AZURE_ACCOUNT_NAME')
AZURE_ACCOUNT_KEY = env.str('AZURE_ACCOUNT_KEY')
AZURE_CONTAINER = env.str('AZURE_CONTAINER')
AZURE_URL_EXPIRATION_SECS = env.int('AZURE_URL_EXPIRATION_SECS', None)
AZURE_OVERWRITE_FILES = True


if 'KOBOCAT_DEFAULT_FILE_STORAGE' in os.environ:
Expand Down