Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
Tester votre fonction localement
Ce guide vous explique comment appeler localement une fonction à partir du code source à l'aide d'une commande gcloud functions. Pour savoir comment développer et tester localement une fonction Cloud Run Functions à l'aide du framework des fonctions, consultez Développement local de fonctions.
Pour prendre en charge l'itération et le débogage rapides des fonctions créées avec l'API Cloud Functions v2, vous pouvez utiliser la commande call dans l'interface de ligne de commande. Cela vous permet d'appeler directement une fonction pour vous assurer qu'elle se comporte comme prévu. La fonction s'exécute immédiatement, même si elle a été déployée pour répondre à un événement spécifique.
Tester votre fonction avec Google Cloud CLI
Pour appeler directement une fonction à l'aide de gcloud CLI, utilisez la commande gcloud functions call et fournissez toutes les données que votre fonction attend au format JSON dans l'argument --data. Exemple :
Exemple de fonction Cloud Pub/Sub basée sur des événements
Cet exemple montre comment appeler directement une fonction basée sur des événements déclenchée par des événements Pub/Sub :
Node.js
constfunctions=require('@google-cloud/functions-framework');// Register a CloudEvent callback with the Functions Framework that will// be executed when the Pub/Sub trigger topic receives a message.functions.cloudEvent('helloPubSub',cloudEvent=>{// The Pub/Sub message is passed as the CloudEvent's data payload.constbase64name=cloudEvent.data.message.data;constname=base64name?Buffer.from(base64name,'base64').toString():'World';console.log(`Hello, ${name}!`);});
Python
importbase64fromcloudevents.httpimportCloudEventimportfunctions_framework# Triggered from a message on a Cloud Pub/Sub topic.@functions_framework.cloud_eventdefsubscribe(cloud_event:CloudEvent)-> None:# Print out the data from Pub/Sub, to prove that it workedprint("Hello, "+base64.b64decode(cloud_event.data["message"]["data"]).decode()+"!")
Go
// Package helloworld provides a set of Cloud Functions samples.packagehelloworldimport("context""fmt""log""github.com/GoogleCloudPlatform/functions-framework-go/functions""github.com/cloudevents/sdk-go/v2/event")funcinit(){functions.CloudEvent("HelloPubSub",helloPubSub)}// MessagePublishedData contains the full Pub/Sub message// See the documentation for more details:// https://cloud.google.com/eventarc/docs/cloudevents#pubsubtypeMessagePublishedDatastruct{MessagePubSubMessage}// PubSubMessage is the payload of a Pub/Sub event.// See the documentation for more details:// https://cloud.google.com/pubsub/docs/reference/rest/v1/PubsubMessagetypePubSubMessagestruct{Data[]byte`json:"data"`}// helloPubSub consumes a CloudEvent message and extracts the Pub/Sub message.funchelloPubSub(ctxcontext.Context,eevent.Event)error{varmsgMessagePublishedDataiferr:=e.DataAs(&msg);err!=nil{returnfmt.Errorf("event.DataAs: %w",err)}name:=string(msg.Message.Data)// Automatically decoded from base64.ifname==""{name="World"}log.Printf("Hello, %s!",name)returnnil}
Java
importcom.google.cloud.functions.CloudEventsFunction;importcom.google.gson.Gson;importfunctions.eventpojos.PubSubBody;importio.cloudevents.CloudEvent;importjava.nio.charset.StandardCharsets;importjava.util.Base64;importjava.util.logging.Logger;publicclassSubscribeToTopicimplementsCloudEventsFunction{privatestaticfinalLoggerlogger=Logger.getLogger(SubscribeToTopic.class.getName());@Overridepublicvoidaccept(CloudEventevent){// The Pub/Sub message is passed as the CloudEvent's data payload.if(event.getData()!=null){// Extract Cloud Event data and convert to PubSubBodyStringcloudEventData=newString(event.getData().toBytes(),StandardCharsets.UTF_8);Gsongson=newGson();PubSubBodybody=gson.fromJson(cloudEventData,PubSubBody.class);// Retrieve and decode PubSub message dataStringencodedData=body.getMessage().getData();StringdecodedData=newString(Base64.getDecoder().decode(encodedData),StandardCharsets.UTF_8);logger.info("Hello, "+decodedData+"!");}}}
require"functions_framework"require"base64"FunctionsFramework.cloud_event"hello_pubsub"do|event|# The event parameter is a CloudEvents::Event::V1 object.# See https://cloudevents.github.io/sdk-ruby/latest/CloudEvents/Event/V1.htmlname=Base64.decode64event.data["message"]["data"]rescue"World"# A cloud_event function does not return a response, but you can log messages# or cause side effects such as sending additional events.logger.info"Hello, #{name}!"end
PHP
use CloudEvents\V1\CloudEventInterface;use Google\CloudFunctions\FunctionsFramework;// Register the function with Functions Framework.// This enables omitting the `FUNCTIONS_SIGNATURE_TYPE=cloudevent` environment// variable when deploying. The `FUNCTION_TARGET` environment variable should// match the first parameter.FunctionsFramework::cloudEvent('helloworldPubsub', 'helloworldPubsub');function helloworldPubsub(CloudEventInterface $event): void{ $log = fopen(getenv('LOGGER_OUTPUT') ?: 'php://stderr', 'wb'); $cloudEventData = $event->getData(); $pubSubData = base64_decode($cloudEventData['message']['data']); $name = $pubSubData ? htmlspecialchars($pubSubData) : 'World'; fwrite($log, "Hello, $name!" . PHP_EOL);}
Pour appeler directement la fonction, envoyez une requête PubsubMessage, qui attend des données encodées en base64 en tant que données d'événement :
Cet exemple de la CLI utilise la syntaxe bash ou sh. Il fonctionne sous Linux et Mac, mais pas sous Windows.
Sauf indication contraire, le contenu de cette page est régi par une licence Creative Commons Attribution 4.0, et les échantillons de code sont régis par une licence Apache 2.0. Pour en savoir plus, consultez les Règles du site Google Developers. Java est une marque déposée d'Oracle et/ou de ses sociétés affiliées.
Dernière mise à jour le 2025/08/19 (UTC).
[[["Facile à comprendre","easyToUnderstand","thumb-up"],["J'ai pu résoudre mon problème","solvedMyProblem","thumb-up"],["Autre","otherUp","thumb-up"]],[["Difficile à comprendre","hardToUnderstand","thumb-down"],["Informations ou exemple de code incorrects","incorrectInformationOrSampleCode","thumb-down"],["Il n'y a pas l'information/les exemples dont j'ai besoin","missingTheInformationSamplesINeed","thumb-down"],["Problème de traduction","translationIssue","thumb-down"],["Autre","otherDown","thumb-down"]],["Dernière mise à jour le 2025/08/19 (UTC)."],[[["\u003cp\u003eCloud Functions v2 API allows direct invocation of functions using the \u003ccode\u003egcloud functions call\u003c/code\u003e command for testing and debugging purposes.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003egcloud functions call\u003c/code\u003e command requires providing function-specific data as JSON within the \u003ccode\u003e--data\u003c/code\u003e argument.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003e--data\u003c/code\u003e argument's content is sent as the body of a POST request for HTTP functions and as the event data for CloudEvent functions.\u003c/p\u003e\n"],["\u003cp\u003eDirect function invocations are designed for testing, not production, and utilize a \u003ccode\u003ecall\u003c/code\u003e method with a limited, non-expandable quota.\u003c/p\u003e\n"],["\u003cp\u003eThe document gives examples of direct invocations for various programming languages such as Node.js, Python, Go, Java, C#, Ruby, and PHP.\u003c/p\u003e\n"]]],[],null,["# Locally test your function\n==========================\n\n| **Warning:** This document only applies to functions that were created with the Cloud Functions v2 API (for example, by using `gcloud functions`, the REST API, or Terraform). If you created a function with the Cloud Run Admin API (for example, by using the Cloud Run Google Cloud console, `gcloud run`, the REST API, or Terraform), see [Compare Cloud Run functions](/run/docs/functions/comparison#comparison).\n\nThis guide shows you how to locally call a function from source code\nusing a `gcloud functions` command. To learn how to locally develop and test a\nCloud Run functions using the Functions Framework, see\n[Local functions development](/run/docs/local-dev-functions).\n\nTo support quick iteration and debugging for functions that were created with\nthe [Cloud Functions v2 API](/functions/docs/apis), you can use the `call`\ncommand in the command-line interface. This lets you directly invoke a function\nto ensure it is behaving as expected. This causes the function to execute\nimmediately, even though it may have been deployed to respond to a specific\nevent.\n| **Note:** Direct function invocations use the [`call` method](/functions/docs/reference/rest/v1/projects.locations.functions/call). This method has a limited [quota](/functions/quotas) that cannot be increased. It is intended for testing and shouldn't be used in production.\n\nTest your function with Google Cloud CLI\n----------------------------------------\n\nTo directly invoke your function using the gcloud CLI, use\nthe `gcloud functions call` command and supply any data\nyour function expects as JSON in the `--data` argument. For example: \n\n gcloud functions call \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-n\"\u003eYOUR_FUNCTION_NAME\u003c/span\u003e\u003c/var\u003e \\\n --region=\u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-n\"\u003eREGION\u003c/span\u003e\u003c/var\u003e --gen2 \\\n --data '{\"name\":\"Kalani\"}'\n\nReplace:\n\n- \u003cvar translate=\"no\"\u003eYOUR_FUNCTION_NAME\u003c/var\u003e: the name of the function you're testing\n- \u003cvar translate=\"no\"\u003eREGION\u003c/var\u003e: the Google Cloud [region](/run/docs/locations) your function is deployed to\n\nThe `--data` argument is sent to your function as follows:\n\n- **For HTTP functions,** the data you supply is sent as the body of a POST request.\n- **For event-driven functions,** the data is passed directly as the event data to your function.\n\nFor more information, see the [`gcloud functions call`](/sdk/gcloud/reference/functions/call) documentation.\n\nCloud Pub/Sub event-driven function example\n-------------------------------------------\n\nThis example shows how to directly invoke an event-driven function\ntriggered by Pub/Sub events: \n\n### Node.js\n\n const functions = require('@google-cloud/functions-framework');\n\n // Register a CloudEvent callback with the Functions Framework that will\n // be executed when the Pub/Sub trigger topic receives a message.\n functions.cloudEvent('helloPubSub', cloudEvent =\u003e {\n // The Pub/Sub message is passed as the CloudEvent's data payload.\n const base64name = cloudEvent.data.message.data;\n\n const name = base64name\n ? Buffer.from(base64name, 'base64').toString()\n : 'World';\n\n console.log(`Hello, ${name}!`);\n });\n\n### Python\n\n import base64\n\n from cloudevents.http import CloudEvent\n import functions_framework\n\n\n # Triggered from a message on a Cloud Pub/Sub topic.\n @functions_framework.cloud_event\n def subscribe(cloud_event: CloudEvent) -\u003e None:\n # Print out the data from Pub/Sub, to prove that it worked\n print(\n \"Hello, \" + base64.b64decode(cloud_event.data[\"message\"][\"data\"]).decode() + \"!\"\n )\n\n### Go\n\n\n // Package helloworld provides a set of Cloud Functions samples.\n package helloworld\n\n import (\n \t\"context\"\n \t\"fmt\"\n \t\"log\"\n\n \t\"github.com/GoogleCloudPlatform/functions-framework-go/functions\"\n \t\"github.com/cloudevents/sdk-go/v2/event\"\n )\n\n func init() {\n \tfunctions.CloudEvent(\"HelloPubSub\", helloPubSub)\n }\n\n // MessagePublishedData contains the full Pub/Sub message\n // See the documentation for more details:\n // https://cloud.google.com/eventarc/docs/cloudevents#pubsub\n type MessagePublishedData struct {\n \tMessage PubSubMessage\n }\n\n // PubSubMessage is the payload of a Pub/Sub event.\n // See the documentation for more details:\n // https://cloud.google.com/pubsub/docs/reference/rest/v1/PubsubMessage\n type PubSubMessage struct {\n \tData []byte `json:\"data\"`\n }\n\n // helloPubSub consumes a CloudEvent message and extracts the Pub/Sub message.\n func helloPubSub(ctx context.Context, e event.Event) error {\n \tvar msg MessagePublishedData\n \tif err := e.DataAs(&msg); err != nil {\n \t\treturn fmt.Errorf(\"event.DataAs: %w\", err)\n \t}\n\n \tname := string(msg.Message.Data) // Automatically decoded from base64.\n \tif name == \"\" {\n \t\tname = \"World\"\n \t}\n \tlog.Printf(\"Hello, %s!\", name)\n \treturn nil\n }\n\n### Java\n\n import com.google.cloud.functions.CloudEventsFunction;\n import com.google.gson.Gson;\n import functions.eventpojos.PubSubBody;\n import io.cloudevents.CloudEvent;\n import java.nio.charset.StandardCharsets;\n import java.util.Base64;\n import java.util.logging.Logger;\n\n public class SubscribeToTopic implements CloudEventsFunction {\n private static final Logger logger = Logger.getLogger(SubscribeToTopic.class.getName());\n\n @Override\n public void accept(CloudEvent event) {\n // The Pub/Sub message is passed as the CloudEvent's data payload.\n if (event.getData() != null) {\n // Extract Cloud Event data and convert to PubSubBody\n String cloudEventData = new String(event.getData().toBytes(), StandardCharsets.UTF_8);\n Gson gson = new Gson();\n PubSubBody body = gson.fromJson(cloudEventData, PubSubBody.class);\n // Retrieve and decode PubSub message data\n String encodedData = body.getMessage().getData();\n String decodedData =\n new String(Base64.getDecoder().decode(encodedData), StandardCharsets.UTF_8);\n logger.info(\"Hello, \" + decodedData + \"!\");\n }\n }\n }\n\n### C#\n\n using CloudNative.CloudEvents;\n using Google.Cloud.Functions.Framework;\n using Google.Events.Protobuf.Cloud.PubSub.V1;\n using Microsoft.Extensions.Logging;\n using System.Threading;\n using System.Threading.Tasks;\n\n namespace HelloPubSub;\n\n public class Function : ICloudEventFunction\u003cMessagePublishedData\u003e\n {\n private readonly ILogger _logger;\n\n public Function(ILogger\u003cFunction\u003e logger) =\u003e\n _logger = logger;\n\n public Task HandleAsync(CloudEvent cloudEvent, MessagePublishedData data, CancellationToken cancellationToken)\n {\n string nameFromMessage = data.Message?.TextData;\n string name = string.IsNullOrEmpty(nameFromMessage) ? \"world\" : nameFromMessage;\n _logger.LogInformation(\"Hello {name}\", name);\n return Task.CompletedTask;\n }\n }\n\n### Ruby\n\n require \"functions_framework\"\n require \"base64\"\n\n FunctionsFramework.cloud_event \"hello_pubsub\" do |event|\n # The event parameter is a CloudEvents::Event::V1 object.\n # See https://cloudevents.github.io/sdk-ruby/latest/CloudEvents/Event/V1.html\n name = Base64.decode64 event.data[\"message\"][\"data\"] rescue \"World\"\n\n # A cloud_event function does not return a response, but you can log messages\n # or cause side effects such as sending additional events.\n logger.info \"Hello, #{name}!\"\n end\n\n### PHP\n\n\n use CloudEvents\\V1\\CloudEventInterface;\n use Google\\CloudFunctions\\FunctionsFramework;\n\n // Register the function with Functions Framework.\n // This enables omitting the `FUNCTIONS_SIGNATURE_TYPE=cloudevent` environment\n // variable when deploying. The `FUNCTION_TARGET` environment variable should\n // match the first parameter.\n FunctionsFramework::cloudEvent('helloworldPubsub', 'helloworldPubsub');\n\n function helloworldPubsub(CloudEventInterface $event): void\n {\n $log = fopen(getenv('LOGGER_OUTPUT') ?: 'php://stderr', 'wb');\n\n $cloudEventData = $event-\u003egetData();\n $pubSubData = base64_decode($cloudEventData['message']['data']);\n\n $name = $pubSubData ? htmlspecialchars($pubSubData) : 'World';\n fwrite($log, \"Hello, $name!\" . PHP_EOL);\n }\n\nTo invoke the function directly, send a\n[`PubsubMessage`](/pubsub/docs/reference/rest/v1/PubsubMessage),\nwhich expects base64-encoded data, as the event data: \n\n### Node.js\n\n```bash\nDATA=$(printf 'Hello!'|base64) && gcloud functions call helloPubSub --data '{\"data\":\"'$DATA'\"}'\n```\n\n### Python\n\n```bash\nDATA=$(printf 'Hello!'|base64) && gcloud functions call hello_pubsub --data '{\"data\":\"'$DATA'\"}'\n```\n\n### Go\n\n```bash\nDATA=$(printf 'Hello!'|base64) && gcloud functions call HelloPubSub --data '{\"data\":\"'$DATA'\"}'\n```\n\n### Java\n\n```bash\nDATA=$(printf 'Hello!'|base64) && gcloud functions call java-hello-pubsub --data '{\"data\":\"'$DATA'\"}'\n```\n\n### C#\n\n```bash\nDATA=$(printf 'Hello!'|base64) && gcloud functions call csharp-hello-pubsub --data '{\"data\":\"'$DATA'\"}'\n```\n\n### Ruby\n\n```bash\nDATA=$(printf 'Hello!'|base64) && gcloud functions call hello_pubsub --data '{\"data\":\"'$DATA'\"}'\n```\n\n### PHP\n\n```bash\nDATA=$(printf 'Hello!'|base64) && gcloud functions call helloworldPubsub --data '{\"data\":\"'$DATA'\"}'\n```\n\nThis CLI example uses `bash` or `sh` syntax. It works in Linux and Mac\nenvironments but not Windows."]]