def get_gcp_project_name():
    Get the project_id from GOOGLE_APPLICATION_CREDENTIALS (if running locally) or (if running on GCF).

    :return bucket_name: str
    # if this is running locally then GOOGLE_APPLICATION_CREDENTIALS should be defined
        with open(os.environ['GOOGLE_APPLICATION_CREDENTIALS'], 'r') as fp:
            credentials = json.load(fp)
        gcp_project = credentials['project_id']
    # If this is running in a cloud function, then get gcp_project from url
        metadata_server = ""
        metadata_flavor = {'Metadata-Flavor' : 'Google'}
        gcp_project = requests.get(metadata_server, headers = metadata_flavor).text
    return gcp_project
downloadDownload PNG downloadDownload JPEG downloadDownload SVG

Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!

Click to optimize width for Twitter