USE_I18N = True USE_L10N = True USE_TZ = True # Static files (CSS, JavaScript, Images) STATIC_URL = '/static/' STATICFILES_DIRS = [ BASE_DIR / "static", ] # STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles') MEDIA_URL = '/media/' MEDIA_ROOT = BASE_DIR / "media" # Google Cloud Storage DEFAULT_FILE_STORAGE = 'storages.backends.gcloud.GoogleCloudStorage' GS_BUCKET_NAME = 'portfolio-gallery' GS_CLASS = 'storages.backends.gcloud.GoogleCloudStorage' GS_CREDENTIALS = service_account.Credentials.from_service_account_file( BASE_DIR / 'avid-bricolage-277519-50dd620c72a0.json') try: from urllib.parse import urljoin, urlparse from storages.backends.gcloud import GoogleCloudStorage gs_url = GoogleCloudStorage().url('') GS_BASE_URL = urljoin(gs_url, urlparse(gs_url).path) except: GS_BASE_URL = "https://storage.googleapis.com/portfolio-gallery/" # Constants THUMBNAIL_SIZE = 293
from rest_framework.exceptions import ValidationError from apps.cases.models import Case from apps.users.models import User from apps.files import models from .storages import CleanStorage ROOT_DIR = settings.ROOT_DIR """Set Up Storage""" if settings.USE_GCS: TEMP_BUCKET = f'{settings.GS_BUCKET_NAME}-test-temp' CASE_BUCKET = f'{settings.GS_BUCKET_NAME}-test-case' TEMP_STORAGE = GoogleCloudStorage(bucket_name=TEMP_BUCKET) CASE_STORAGE = GoogleCloudStorage(bucket_name=CASE_BUCKET) else: temp_dir = f'{settings.MEDIA_ROOT}/test-temp/' case_dir = f'{settings.MEDIA_ROOT}/test-casefile/' TEMP_STORAGE = FileSystemStorage(location=temp_dir, base_url=temp_dir) CASE_STORAGE = FileSystemStorage(location=case_dir, base_url=case_dir) if not os.path.exists(temp_dir): os.mkdir(temp_dir) if not os.path.exists(case_dir): os.mkdir(case_dir) class CleanStorageTestCase(TestCase): def setUp(self): """Create User"""
def delete_bucket(name): client = GoogleCloudStorage().client bucket = client.bucket(name) bucket.delete(force=True)
from storages.backends.gcloud import GoogleCloudStorage MediaRootGoogleCloudStorage = lambda: GoogleCloudStorage(location= 'chari-lake/media')
def select_storage(): return default_storage if settings.DEBUG else GoogleCloudStorage()
def create_bucket(name): client = GoogleCloudStorage().client bucket = client.bucket(name) bucket.location = settings.GCP_BUCKET_LOCATION bucket.iam_configuration.uniform_bucket_level_access_enabled = True client.create_bucket(bucket)
) LOGO_STORAGE = RackspaceStorage( location="%spicture/logo/" % ROOT_URL, base_url=BASE_URL, purpose="LOGO_PHOTO_SIZES", ) PICTURE_STORAGE = RackspaceStorage( location="%spicture/others/" % ROOT_URL, base_url=BASE_URL, purpose="PICTURE_PHOTO_SIZES", ) elif USE_GCS: VIDEO_STORAGE = GoogleCloudStorage(location="%svideo" % ROOT_URL, file_overwrite=False) FILE_STORAGE = GoogleCloudStorage(location="%sfile" % ROOT_URL, file_overwrite=False) AVATAR_STORAGE = GoogleCloudStorage(location="%spicture/avatar" % ROOT_URL, file_overwrite=False) COVER_STORAGE = GoogleCloudStorage(location="%spicture/cover" % ROOT_URL, file_overwrite=False) LOGO_STORAGE = GoogleCloudStorage(location="%spicture/logo" % ROOT_URL, file_overwrite=False) PICTURE_STORAGE = GoogleCloudStorage(location="%spicture/others" % ROOT_URL, file_overwrite=False) elif USE_S3: VIDEO_STORAGE = S3Boto3Storage(location='%svideo' % ROOT_URL, file_overwrite=False)
def __init__(self): self._gcp = GoogleCloudStorage()
def open(self, mode='rb'): storage = GoogleCloudStorage(bucket_name=self.bucket_name()) return storage.open(self.key(), mode=mode)
def get_client(): return GoogleCloudStorage().client
def url(self): storage = GoogleCloudStorage(bucket_name=self.bucket_name()) return storage.url(self.key())
from django.db import models from storages.backends.gcloud import GoogleCloudStorage from multiselectfield import MultiSelectField # Create your models here. # from django.core.files.storage import DEFAULT_FILE_STORAGE storage = GoogleCloudStorage() # DEFAULT_FILE_STORAGE = 'storages.backends.gcloud.GoogleCloudStorage' class Upload(models.Model): @staticmethod def upload_image(file, filename): try: target_path = '/images/' + filename path = storage.save(target_path, file) return storage.url(path) except Exception as e: print("Failed to upload!") class Matching(models.Model): name = models.CharField(max_length = 100) sell_topics = models.CharField(max_length = 100) buy_topics = models.CharField(max_length = 100) city = models.CharField(max_length = 100) study_topics = models.CharField(max_length = 100) email = models.EmailField(max_length = 254, default = 'None') sell_price = models.DecimalField(decimal_places = 2, max_digits=100, default=10.00) #books to sell, books to buy, topics to study together, location of residence
from collectfast.storage_extensions import get_storage_extensions from collectfast.storage_extensions.gcloud import GoogleCloudStorageExtensions from collectfast.storage_extensions.s3boto import S3BotoStorageExtensions from collectfast.storage_extensions.s3boto3 import S3Boto3StorageExtensions from .utils import test class UnknownStorage(Storage): def __init__(self): super(UnknownStorage, self).__init__() pass class InheritedStorage(GoogleCloudStorage): pass @test def test_get_storage_extensions(case): case.assertIsInstance(get_storage_extensions(S3BotoStorage()), S3BotoStorageExtensions) case.assertIsInstance(get_storage_extensions(S3Boto3Storage()), S3Boto3StorageExtensions) case.assertIsInstance(get_storage_extensions(GoogleCloudStorage()), GoogleCloudStorageExtensions) case.assertIsInstance(get_storage_extensions(InheritedStorage()), GoogleCloudStorageExtensions) with case.assertRaises(RuntimeError): get_storage_extensions(UnknownStorage())