0

I'm deploying my Django app to Railway.app and am hosting media on GCS (I like the free tier). I've created the service account and received the .json of the credentials. On my local system for dev, I have the .json file in the directory and refer to it like this:

GS_CREDENTIALS = service_account.Credentials.from_service_account_file(".gcs_credentials.json")

No problems and files upload great. I'm not passing the credentials file through Github (for safety reasons) but now I'm stuck.

Is there a way to save this JSON file as an environment variable in Railway.app?

For all of my other variables, I'm using django-environ and everything is working great. I just can't find anything online about passing credentials in JSON form to a deployment on Railway.

Thank you in advance.

Thomas
  • 1
  • 1

1 Answers1

0

Someone from Reddit was able to answer this for me!

So I had to save the json credentials in my .env file like this (I had to put everything on the same line):

DJANGO_GS_CREDENTIALS={"type": "service_account","project_id": "xyz","private_key_id": "xyz","private_key": "-----BEGIN PRIVATE KEY---.....

Then, in settings.py, I referred to it using this:

gs_json_data = json.loads(env("DJANGO_GS_CREDENTIALS"))
gs_json_data["private_key"] = gs_json_data["private_key"].replace("\\n", "\n")
GS_CREDENTIALS = service_account.Credentials.from_service_account_info(gs_json_data)
Thomas
  • 1
  • 1
  • I've seen many state you should first encode the json contents prior to storing as an environment variable, then decode within your application. – Ryan Prentiss Jul 25 '23 at 23:01