Tobi
Tobi

Reputation: 1902

Where / how to store API keys in Google Colabs securely?

If I share for example a Google Colab ipynb with other I don't want them to be able to access my API keys. In AWS there would be something like SecureString in Parameter Store for Lambdas. Is there something similar to use with Google Colabs? Thanks

Upvotes: 3

Views: 3634

Answers (3)

arainchi
arainchi

Reputation: 1492

You want to use Google Colab Secrets feature:

enter image description here

google.colab.userdata allows you to get those Secrets and assign to environment variables:

import os
from google.colab import userdata

os.environ["AWS_SECRET_KEY"]=userdata.get('AWS_SECRET_KEY')
os.environ["AWS_ACCESS_KEY"]=userdata.get('AWS_ACCESS_KEY')

Upvotes: 2

Tobi
Tobi

Reputation: 1902

I now do it like that

drive.mount('/content/drive')
with open('testLALALALA.json') as jsonfile:
    KEY_DATA = json.load(jsonfile)


def initialize_analyticsreporting():
  credentials = ServiceAccountCredentials.from_json_keyfile_dict(KEY_DATA, SCOPES)
  analytics = build('analyticsreporting', 'v4', credentials=credentials)
  return analytics

Upvotes: 3

korakot
korakot

Reputation: 40858

You can save your key in Google Drive. Then mount and load it.

Other people can mount and load their own.

Upvotes: 1

Related Questions