django-storages
django-storages copied to clipboard
Support for Oracle Object Storage
Hi All,
Is there any support for Oracle Object Storage, We have client who is insisting to host on oracle.
OCI Object Storage is full compatible with AWS S3 API.
You can check this https://medium.com/oracledevs/use-aws-python-sdk-to-upload-files-to-oracle-cloud-infrastructure-object-storage-b623e5681412
And this https://testdriven.io/blog/storing-django-static-and-media-files-on-amazon-s3/
After you apply that tutorials, the following snippet could be useful:
pip install boto3 django-storages
# settings.py
ORACLE_BUCKET_NAME = 'myOciBucketName'
ORACLE_BUCKET_NAMESPACE = 'hsrt37fxm24t'
ORACLE_REGION = 'sa-saopaulo-1'
AWS_ACCESS_KEY_ID = os.environ.get('ORACLE_ACCESS_KEY')
AWS_SECRET_ACCESS_KEY = os.environ.get('ORACLE_CUSTOMER_SECRET_KEY')
AWS_STORAGE_BUCKET_NAME = ORACLE_BUCKET_NAME
AWS_S3_CUSTOM_DOMAIN = f"{ORACLE_BUCKET_NAMESPACE}.compat.objectstorage.{ORACLE_REGION}.oraclecloud.com"
AWS_S3_ENDPOINT_URL = f"https://{AWS_S3_CUSTOM_DOMAIN}"
AWS_S3_OBJECT_PARAMETERS = {
'CacheControl': 'max-age=86400',
}
DEFAULT_FILE_STORAGE = 'core.storage_backends.PublicMediaStorage'
MEDIA_URL = f"https://{AWS_S3_CUSTOM_DOMAIN}/{ORACLE_BUCKET_NAME}/media"
STATICFILES_STORAGE = 'core.storage_backends.StaticStorage'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'upload/static'),
]
STATIC_URL = f"https://{AWS_S3_CUSTOM_DOMAIN}/{ORACLE_BUCKET_NAME}/static"
This is a example of custom backend
from datetime import datetime, timedelta
from urllib.parse import urlencode
from django.conf import settings
from django.utils.encoding import filepath_to_uri
from storages.backends.s3boto3 import S3Boto3Storage
class OracleStorage(S3Boto3Storage):
def url(self, name, parameters=None, expire=None, http_method=None):
# Preserve the trailing slash after normalizing the path.
name = self._normalize_name(self._clean_name(name))
params = parameters.copy() if parameters else {}
if expire is None:
expire = self.querystring_expire
if not self.custom_domain:
raise Exception('Must set AWS_S3_CUSTOM_DOMAIN')
if not settings.ORACLE_BUCKET_NAME:
raise Exception('Must set ORACLE_BUCKET_NAME')
url = '{}//{}/{}/{}{}'.format(
self.url_protocol,
self.custom_domain,
settings.ORACLE_BUCKET_NAME,
filepath_to_uri(name),
'?{}'.format(urlencode(params)) if params else '',
)
if self.querystring_auth and self.cloudfront_signer:
expiration = datetime.utcnow() + timedelta(seconds=expire)
return self.cloudfront_signer.generate_presigned_url(url, date_less_than=expiration)
return url
class StaticStorage(OracleStorage):
location = 'static'
default_acl = 'public-read'
class PublicMediaStorage(OracleStorage):
location = 'media'
default_acl = 'public-read'
file_overwrite = False
It is S3 compat and docs exists for it now!