-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathstorage.py
More file actions
97 lines (77 loc) · 3.33 KB
/
storage.py
File metadata and controls
97 lines (77 loc) · 3.33 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import hashlib
from django.conf import settings
# Fallbacks
PublicStorageClass = object
PrivateStorageClass = object
# Import only the required backend
if settings.STORAGE_IS_S3:
try:
from storages.backends.s3boto3 import S3Boto3Storage
class PublicStorageClass(S3Boto3Storage):
bucket_name = getattr(settings, "AWS_STORAGE_BUCKET_NAME", None)
custom_domain = getattr(settings, "AWS_S3_PUBLIC_CUSTOM_DOMAIN", "")
class PrivateStorageClass(S3Boto3Storage):
bucket_name = getattr(settings, "AWS_STORAGE_PRIVATE_BUCKET_NAME", None)
except ImportError:
raise RuntimeError("S3 backend requested but 'boto3' or 's3boto3' storage is not installed")
elif settings.STORAGE_IS_GCS:
try:
from storages.backends.gcloud import GoogleCloudStorage
class PublicStorageClass(GoogleCloudStorage):
bucket_name = getattr(settings, "GS_PUBLIC_BUCKET_NAME", None)
class PrivateStorageClass(GoogleCloudStorage):
bucket_name = getattr(settings, "GS_PRIVATE_BUCKET_NAME", None)
except ImportError:
raise RuntimeError("GCS backend requested but 'google-cloud-storage' is not installed")
elif settings.STORAGE_IS_AZURE:
try:
from storages.backends.azure_storage import AzureStorage
class CodalabAzureStorage(AzureStorage):
def __init__(self, *args, azure_container=None, **kwargs):
if azure_container:
self.azure_container = azure_container
super().__init__(*args, **kwargs)
class PublicStorageClass(CodalabAzureStorage):
def __init__(self, *args, **kwargs):
super().__init__(
*args,
azure_container=settings.AZURE_CONTAINER,
account_name=settings.AZURE_ACCOUNT_NAME,
account_key=settings.AZURE_ACCOUNT_KEY,
**kwargs,
)
class PrivateStorageClass(CodalabAzureStorage):
def __init__(self, *args, **kwargs):
super().__init__(
*args,
azure_container=settings.BUNDLE_AZURE_CONTAINER,
account_name=settings.BUNDLE_AZURE_ACCOUNT_NAME,
account_key=settings.BUNDLE_AZURE_ACCOUNT_KEY,
**kwargs,
)
except ImportError:
raise RuntimeError("Azure backend requested but 'azure-storage-blob' is not installed")
else:
raise NotImplementedError("Must use STORAGE_TYPE of 's3', 'minio', 'gcs', or 'azure'")
# Instantiate the storages
try:
from django.core.files.storage import storages
# This one is safe to access — assumes STORAGES["default"] is set correctly
DefaultStorage = storages["default"]
except Exception as e:
raise RuntimeError(f"Failed to load default storage from Django STORAGES: {e}")
try:
BundleStorage = PrivateStorageClass()
except Exception:
BundleStorage = DefaultStorage
try:
PublicStorage = PublicStorageClass()
except Exception:
PublicStorage = DefaultStorage
def md5(filename):
"""Given some file return its md5, works well on large files"""
hash_md5 = hashlib.md5()
with open(filename, "rb") as f:
for chunk in iter(lambda: f.read(4096), b""):
hash_md5.update(chunk)
return hash_md5.hexdigest()