[PYTHON] How to call Cloud API from GCP Cloud Functions

I wanted to call a function that is not libraryed in the Python SDK from Cloud Functions, but since the gcloud command is not in Cloud Functions (gcloud is not installed on the compute node where Cloud Functions runs), I had to call the Cloud API directly.

If you want to call Cloud API from Cloud Functions by REST, you can call it by authenticating as follows.

def hello_world(request):
    import google.auth
    from google.auth.transport import requests

    # Get the credentials and project ID from the environment.
    credentials, project = google.auth.default(
        scopes=['https://www.googleapis.com/auth/cloud-platform'])

    # Create a requests Session object with the credentials.
    session = requests.AuthorizedSession(credentials)

    # Make an authenticated API request
    response = session.get(
        'https://www.googleapis.com/storage/v1/b'.format(project),
        params={'project': project})
    response.raise_for_status()

    retrun response.text

For testing the API alone, a test form is prepared, so this is convenient https://cloud.google.com/storage/docs/json_api/v1/buckets/list

Recommended Posts

How to call Cloud API from GCP Cloud Functions
GCP: Repeat from Pub / Sub to Cloud Functions, Cloud Functions to Pub / Sub
[GCP] How to output Cloud Functions log to Cloud Logging (Stackdriver Logging) (Python)
How to use GCP's Cloud Vision API
How to connect to Cloud Firestore from Google Cloud Functions with python code
How to use the Google Cloud Translation API
How to enable python3 to run when sending jobs from GCP Cloud Composer to Dataflow
[GCP] [Python] Deploy API serverless with Google Cloud Functions!
How to post a ticket from the Shogun API
[Python] How to call a c function from python (ctypes)
How to call a function
Emulate GCP Cloud functions locally
Call C language functions from Python to exchange multidimensional arrays
How to hide your Google Maps API key from HTML
Let's use Watson from Python! --How to use Developer Cloud Python SDK
How to call Python or Julia from Ruby (experimental implementation)
Call Matlab from Python to optimize
How to use SWIG from waf
How to use OpenPose's Python API
How to launch Explorer from WSL
Ssh connect to GCP from Windows
How to access wikipedia from python
How to use bing search api
How to convert from .mgz to .nii.gz
[Python] How to use Typetalk API
How to call PyTorch in Julia
[GCP] How to publish Cloud Storage signed URLs (temporary URLs) in Python
How to get followers and followers from python using the Mastodon API
Post a message from IBM Cloud Functions to Slack in Python
How to update the python version of Cloud Shell on GCP
How to make Word Cloud characters monochromatic
From the introduction of GoogleCloudPlatform Natural Language API to how to use it
How to create a clone from Github
How to easily convert format from Markdown
I tried to extract characters from subtitles (OpenCV: Google Cloud Vision API)
How to update Google Sheets from Python
[TF] How to use Tensorboard from Keras
Use Google Cloud Vision API from Python
How to access RDS from Lambda (python)
How to operate Linux from the console
How to get a sample report from a hash value using VirusTotal's API
How to switch python versions in cloud9
How to easily operate IOT home appliances from Siri by API hacking
How to create a repository from media
[Python] Understand how to use recursive functions
How to access the Datastore from the outside
[Rails] How to detect radical images by analyzing images using Cloud Vision API
API explanation to touch mastodon from python
How to build an application from the cloud using the Django web framework
Connect to coincheck's Websocket API from Python
How to automatically generate API document with Django REST framework & POST from document screen
How to use GCP trace with open Telemetry
How to open a web browser from python
How to create a function object from a string
Text extraction with GCP Cloud Vision API (Python3.6)
How to create explanatory variables and objective functions
How to get results from id in Celery
[Python] How to read data from CIFAR-10 and CIFAR-100
How to generate a Python object from JSON
How to deploy a Streamlit application to GCP (GAE)
How to operate Linux from the outside Procedure