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