Release: 'Run Saleor with Google Cloud Storage'
Pre-release
Pre-release
CHANGELOG
DEPENDENCIES
-
Install the package: django-storages[google]
-
Rerun pipenv to relock all of the packages in file using the -r flag
$ pipenv install django-storages[google]
(env)$ pipenv lock -r > requirements.txt
(env)$ pipenv lock -r -d -r requirements.txt
- Also run pipenv so that the Pipfile and Pipfile.lock are both updated.
(env)$ pipenv install --selective-upgrade --verbose gunicorn django-storages[google]
- Lines Added in Saleor latest package requirements.txt that become this requirements.txt. This need to be done manually as the issue on freeze does case sensitive comparsion hasn't been fixed
cachetools==3.1.0
google-api-core==1.9.0
google-auth==1.6.3
google-cloud-core==0.29.1
google-cloud-storage==1.15.0
google-resumable-media==0.3.2
googleapis-common-protos==1.5.9
protobuf==3.7.1
pyasn1==0.4.5
pyasn1-modules==0.2.5
rsa==4.0
Image will be of signed URLs, which you use to give time-limited resource access to anyone in possession of the URL
REQUIREMENTS
- Set credential json file
- Create a service account. Make sure your service account has access to the bucket.
- Create the key and download XXX.json file and put it somewhere in project directory
- Set an environment variable of GOOGLE_APPLICATION_CREDENTIALS to the path of json file.
- Optional. If Step 3 doesn't work then change the environment variable name to GS_CREDENTIALS
- Add the environment variable to include the followings
STATIC_URL = 'https://storage.googleapis.com/{}/'.format(GS_STATIC_BUCKET_NAME)
MEDIA_URL = 'https://storage.googleapis.com/{}/'.format(GS_MEDIA_BUCKET_NAME)
- Modified files
- Pipfile
- Pipfile.lock
- requirements.txt
- requirements_dev.txt
- saleor/settings.py
- saleor/core/storages.py
- Edit on the header of settings.py to include the followings
from datetime import timedelta
from google.oauth2 import service_account
- and add the following codes refer to this changes
# Google Cloud Storage configuration
GS_PROJECT_ID = os.environ.get("GS_PROJECT_ID")
GS_STORAGE_BUCKET_NAME = os.environ.get("GS_STORAGE_BUCKET_NAME")
GS_MEDIA_BUCKET_NAME = os.environ.get("GS_MEDIA_BUCKET_NAME")
GS_AUTO_CREATE_BUCKET = get_bool_from_env("GS_AUTO_CREATE_BUCKET", False)
# If GOOGLE_APPLICATION_CREDENTIALS is set there is no need to load OAuth token
# See https://django-storages.readthedocs.io/en/latest/backends/gcloud.html
if "GOOGLE_APPLICATION_CREDENTIALS" not in os.environ:
GS_CREDENTIALS = os.environ.get("GS_CREDENTIALS")
if AWS_STORAGE_BUCKET_NAME:
STATICFILES_STORAGE = "storages.backends.s3boto3.S3Boto3Storage"
elif GS_STORAGE_BUCKET_NAME:
STATICFILES_STORAGE = "storages.backends.gcloud.GoogleCloudStorage"
if AWS_MEDIA_BUCKET_NAME:
DEFAULT_FILE_STORAGE = "saleor.core.storages.S3MediaStorage"
THUMBNAIL_DEFAULT_STORAGE = DEFAULT_FILE_STORAGE
elif GS_MEDIA_BUCKET_NAME:
DEFAULT_FILE_STORAGE = "saleor.core.storages.GCSMediaStorage"
THUMBNAIL_DEFAULT_STORAGE = DEFAULT_FILE_STORAGE
- Change
saleor/core/storages.py
refer to this code
from django.conf import settings
from storages.backends.gcloud import GoogleCloudStorage
from storages.backends.s3boto3 import S3Boto3Storage
class S3MediaStorage(S3Boto3Storage):
def __init__(self, *args, **kwargs):
self.bucket_name = settings.AWS_MEDIA_BUCKET_NAME
self.custom_domain = settings.AWS_MEDIA_CUSTOM_DOMAIN
super().__init__(*args, **kwargs)
class GCSMediaStorage(GoogleCloudStorage):
def __init__(self, *args, **kwargs):
self.bucket_name = settings.GS_MEDIA_BUCKET_NAME
super().__init__(*args, **kwargs)```
TROUBLESHOOTING
- In case a source doesn't work for you, you can fix it as following steps
- Fork the original source, modify locally and push it back there,
- Then replace the package using your repo like the sample below
$ pip install --upgrade pip
$ pip install -U virtualenv
$ rm -rf ~/.user/virtual-env
$ mv virtualenv ~/.user/virtual-env
$ source ~/.user/virtual-env/bin/activate
(virtual-env)$ pip install -U https://github.com/user/project/archive/develop.zip
(virtual-env)$ pip freeze > requirements.txt
- You may use
expect
to simplify git login interaction as an alternatif to ssh-agent.
$ cd /path/to/your/git/folder
$ push
- You may also need the following tools to check the syntax of Python script without executing
(virtual-env)$ pip install pylint
ANNOUNCEMENTS
In future releases this project will be implemented in GitHub Actions you may learn and join.