Cloud Functions: グリーティング関数
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
このサンプルでは、姓と名を入力として受け取り、パーソナライズされた挨拶メッセージを返す Cloud Functions の関数を作成する方法を示します。
コードサンプル
C#
Cloud Run functions に対する認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の認証を設定するをご覧ください。
Go
Cloud Run functions に対する認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の認証を設定するをご覧ください。
Java
Cloud Run functions に対する認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の認証を設定するをご覧ください。
Node.js
Cloud Run functions に対する認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の認証を設定するをご覧ください。
PHP
Cloud Run functions に対する認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の認証を設定するをご覧ください。
Python
Cloud Run functions に対する認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の認証を設定するをご覧ください。
Ruby
Cloud Run functions に対する認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の認証を設定するをご覧ください。
特に記載のない限り、このページのコンテンツはクリエイティブ・コモンズの表示 4.0 ライセンスにより使用許諾されます。コードサンプルは Apache 2.0 ライセンスにより使用許諾されます。詳しくは、Google Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
[[["わかりやすい","easyToUnderstand","thumb-up"],["問題の解決に役立った","solvedMyProblem","thumb-up"],["その他","otherUp","thumb-up"]],[["わかりにくい","hardToUnderstand","thumb-down"],["情報またはサンプルコードが不正確","incorrectInformationOrSampleCode","thumb-down"],["必要な情報 / サンプルがない","missingTheInformationSamplesINeed","thumb-down"],["翻訳に関する問題","translationIssue","thumb-down"],["その他","otherDown","thumb-down"]],[],[[["\u003cp\u003eThis page demonstrates the creation of a Cloud Function that takes a first and last name as input.\u003c/p\u003e\n"],["\u003cp\u003eThe Cloud Function returns a personalized greeting message using the provided first and last name.\u003c/p\u003e\n"],["\u003cp\u003eCode samples for the greeting function are provided in C#, Go, Java, Node.js, PHP, Python, and Ruby.\u003c/p\u003e\n"],["\u003cp\u003eThe document specifies how to set up Application Default Credentials for authentication to Cloud Run functions.\u003c/p\u003e\n"]]],[],null,["# Cloud Functions: Greeting Function\n\nThis sample demonstrates how to create a Cloud Function that accepts a first name and last name as input, and returns a personalized greeting message.\n\nCode sample\n-----------\n\n### C#\n\n\nTo authenticate to Cloud Run functions, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n using Google.Cloud.Functions.Framework;\n using System.Text.Json.Serialization;\n using System.Threading;\n using System.Threading.Tasks;\n\n namespace Greeting;\n\n public class Function : ITypedFunction\u003cGreetingRequest, GreetingResponse\u003e\n {\n public Task\u003cGreetingResponse\u003e HandleAsync(GreetingRequest request, CancellationToken cancellationToken) =\u003e\n Task.FromResult(new GreetingResponse\n {\n Message = $\"Hello {request.FirstName} {request.LastName}!\",\n });\n }\n\n public class GreetingRequest\n {\n [JsonPropertyName(\"first_name\")]\n public string FirstName { get; set; }\n\n [JsonPropertyName(\"last_name\")]\n public string LastName { get; set; }\n }\n\n public class GreetingResponse\n {\n [JsonPropertyName(\"message\")]\n public string Message { get; set; }\n }\n\n### Go\n\n\nTo authenticate to Cloud Run functions, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n\n // Package greeting provides a set of Cloud Functions samples.\n package greeting\n\n import (\n \t\"fmt\"\n\n \t\"github.com/GoogleCloudPlatform/functions-framework-go/functions\"\n )\n\n func init() {\n \tfunctions.Typed(\"Greeting\", greeting)\n }\n\n // greeting is a Typed Cloud Function.\n func greeting(request *GreetingRequest) (*GreetingResponse, error) {\n \treturn &GreetingResponse{\n \t\tMessage: fmt.Sprintf(\"Hello %v %v!\", request.FirstName, request.LastName),\n \t}, nil\n }\n\n type GreetingRequest struct {\n \tFirstName string `json:\"first_name\"`\n \tLastName string `json:\"last_name\"`\n }\n\n type GreetingResponse struct {\n \tMessage string `json:\"message\"`\n }\n\n### Java\n\n\nTo authenticate to Cloud Run functions, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n import com.google.cloud.functions.TypedFunction;\n import com.google.gson.annotations.SerializedName;\n\n public class Greeting implements TypedFunction\u003cGreetingRequest, GreetingResponse\u003e {\n @Override\n public GreetingResponse apply(GreetingRequest request) throws Exception {\n GreetingResponse response = new GreetingResponse();\n response.message = String.format(\"Hello %s %s!\", request.firstName, request.lastName);\n return response;\n }\n }\n\n### Node.js\n\n\nTo authenticate to Cloud Run functions, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n const functions = require('@google-cloud/functions-framework');\n\n functions.typed('greeting', req =\u003e {\n return {\n message: `Hello ${req.first_name} ${req.last_name}!`,\n };\n });\n\n### PHP\n\n\nTo authenticate to Cloud Run functions, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n\n use Google\\CloudFunctions\\FunctionsFramework;\n\n class GreetingRequest\n {\n /** @var string */\n public $first_name;\n\n /** @var string */\n public $last_name;\n\n public function __construct(string $first_name = '', string $last_name = '')\n {\n $this-\u003efirst_name = $first_name;\n $this-\u003elast_name = $last_name;\n }\n\n public function serializeToJsonString(): string\n {\n return json_encode([\n 'first_name' =\u003e $this-\u003efirst_name,\n 'last_name' =\u003e $this-\u003elast_name,\n ]);\n }\n\n public function mergeFromJsonString(string $body): void\n {\n $obj = json_decode($body);\n $this-\u003efirst_name = $obj['first_name'];\n $this-\u003elast_name = $obj['last_name'];\n }\n }\n\n class GreetingResponse\n {\n /** @var string */\n public $message;\n\n public function __construct(string $message = '')\n {\n $this-\u003emessage = $message;\n }\n\n public function serializeToJsonString(): string\n {\n return json_encode([\n 'message' =\u003e $message,\n ]);\n }\n\n public function mergeFromJsonString(string $body): void\n {\n $obj = json_decode($body);\n $this-\u003emessage = $obj['message'];\n }\n };\n\n function greeting(GreetingRequest $req): GreetingResponse\n {\n return new GreetingResponse(\"Hello $req-\u003efirst_name $req-\u003elast_name!\");\n };\n\n FunctionsFramework::typed('greeting', 'greeting');\n\n### Python\n\n\nTo authenticate to Cloud Run functions, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n from dataclasses import dataclass\n\n import functions_framework\n\n\n @dataclass\n class GreetingRequest:\n first_name: str\n last_name: str\n\n # Required to deserialize the request\n @staticmethod\n def from_dict(req: dict) -\u003e \"GreetingRequest\":\n return GreetingRequest(\n first_name=req[\"first_name\"],\n last_name=req[\"last_name\"],\n )\n\n\n @dataclass\n class GreetingResponse:\n message: str\n\n # Required to serialize the response\n def to_dict(self) -\u003e dict:\n return {\n \"message\": self.message,\n }\n\n\n @functions_framework.typed\n def greeting(req: GreetingRequest):\n return GreetingResponse(message=f\"Hello {req.first_name} {req.last_name}!\")\n\n### Ruby\n\n\nTo authenticate to Cloud Run functions, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n require \"functions_framework\"\n\n FunctionsFramework.typed \"greeting\" do |req|\n {\n message: \"Hello #{req['first_name']} #{req['last_name']}!\"\n }\n end\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=functions)."]]