Skip to content

Release: 'Run Saleor with Google Cloud Storage'

Pre-release
Pre-release
Compare
Choose a tag to compare
@Chetabahana Chetabahana released this 28 Apr 17:14
· 965 commits to master since this release

CHANGELOG

Google Cloud Storage

DEPENDENCIES

$ pipenv install django-storages[google]
(env)$ pipenv lock -r > requirements.txt
(env)$ pipenv lock -r -d -r requirements.txt
(env)$ pipenv install --selective-upgrade --verbose gunicorn django-storages[google]
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

Saleor is running with Google bucket

REQUIREMENTS

  • Set credential json file
  1. Create a service account. Make sure your service account has access to the bucket.
  2. Create the key and download XXX.json file and put it somewhere in project directory
  3. Set an environment variable of GOOGLE_APPLICATION_CREDENTIALS to the path of json file.
  4. 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
  1. Pipfile
  2. Pipfile.lock
  3. requirements.txt
  4. requirements_dev.txt
  5. saleor/settings.py
  6. 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
# 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

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
  1. Fork the original source, modify locally and push it back there,
  2. 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
$ cd /path/to/your/git/folder
$ push

use expect as an alternatif to ssh-agent for git login interaction in cygwin.

  • You may also need the following tools to check the syntax of Python script without executing
  1. PyChecker
  2. Pyflakes
  3. Pylint
(virtual-env)$ pip install pylint

pylint

UPDATE
As of May 2019
screencapture-mail-google-mail-u-0-2019-05-23-16_19_23

ANNOUNCEMENTS

In future releases this project will be implemented in GitHub Actions you may learn and join.