呼叫本機函式

設定

本文假設您已使用 Functions FrameworkBuildpackslocalhost設定本機執行的函式。這也假設您已在本機電腦上安裝 curl 工具。

將要求傳送至本機函式

您可以透過區域服務程序,傳送 HTTP 要求來觸發區域執行的函式。

如要判斷函式在本機運作的地點,請查看您開始執行函式時顯示的網址。根據預設,函式會託管在 localhost:8080

HTTP 函式

當您透過開發環境測試 HTTP 函式時,通常會在 localhost:8080 上監聽要求。這個介面只能透過函式執行所在的機器或 VM 存取;從任何其他系統傳送的要求都無法存取。因此,您必須從函式執行的系統發出 HTTP 要求。在下列範例中,如果函式監聽的通訊埠不是 8080,請將範例中的 8080 替換為函式的通訊埠號碼。

使用 Cloud Shell 測試 HTTP 函式

如果您使用 Cloud Shell 建構及測試函式,請在 Cloud Shell 終端機視窗中本機啟動函式,然後透過瀏覽器或 curl 執行個體發出 HTTP 觸發要求,如下所示:

瀏覽器

按一下 Cloud Shell 工具列上的 網頁預覽功能按鈕 圖示,然後選擇「port 8080」或「Change port」,即可選取其他通訊埠。這會在正確的系統上開啟瀏覽器視窗,並向指定的通訊埠發出 GET 要求。

Curl

如要控制 HTTP 要求的格式,或查看未格式化的回覆,請使用 curl

  1. 在 Cloud Shell 選單列中按一下「+」圖示,即可在執行函式的相同系統上開啟新的終端機視窗。
  2. 在該視窗中執行 curl 指令,觸發函式。例如:

    curl localhost:8080
    

在電腦伺服器上測試 HTTP 函式

如果您要在本機電腦系統上建構及執行函式,請先在本機啟動函式,然後透過瀏覽器或 curl 執行個體發出 HTTP 觸發要求,如下所示:

瀏覽器

開啟新的瀏覽器視窗或分頁,然後在瀏覽器網址列中輸入 http://localhost:8080。這會開啟瀏覽器視窗,前往桌面伺服器上的 localhost:8080 觸發函式。

Curl

在本機電腦的桌面上開啟新的終端機視窗,然後在該視窗中執行 curl 指令,觸發函式。例如:

 curl localhost:8080

這會執行指定的 curl 指令,觸發函式並顯示未格式化的回應。

CloudEvent 函式

您可以使用 curl 將範例事件傳送至 CloudEvent 函式。下列 curl 要求說明如何將 Cloud Pub/SubCloud Storage 事件範例傳送至在 localhost:8080 執行的 CloudEvent 函式。

Pub/Sub

curl localhost:8080 \
  -X POST \
  -H "Content-Type: application/json" \
  -H "ce-id: 123451234512345" \
  -H "ce-specversion: 1.0" \
  -H "ce-time: 2020-01-02T12:34:56.789Z" \
  -H "ce-type: google.cloud.pubsub.topic.v1.messagePublished" \
  -H "ce-source: //pubsub.googleapis.com/projects/MY-PROJECT/topics/MY-TOPIC" \
  -d '{
        "message": {
          "data": "d29ybGQ=",
          "attributes": {
             "attr1":"attr1-value"
          }
        },
        "subscription": "projects/MY-PROJECT/subscriptions/MY-SUB"
      }'
    

儲存空間

curl localhost:8080 \
  -X POST \
  -H "Content-Type: application/json" \
  -H "ce-id: 123451234512345" \
  -H "ce-specversion: 1.0" \
  -H "ce-time: 2020-01-02T12:34:56.789Z" \
  -H "ce-type: google.cloud.storage.object.v1.finalized" \
  -H "ce-source: //storage.googleapis.com/projects/_/buckets/MY-BUCKET-NAME" \
  -H "ce-subject: objects/MY_FILE.txt" \
  -d '{
        "bucket": "MY_BUCKET",
        "contentType": "text/plain",
        "kind": "storage#object",
        "md5Hash": "...",
        "metageneration": "1",
        "name": "MY_FILE.txt",
        "size": "352",
        "storageClass": "MULTI_REGIONAL",
        "timeCreated": "2020-04-23T07:38:57.230Z",
        "timeStorageClassUpdated": "2020-04-23T07:38:57.230Z",
        "updated": "2020-04-23T07:38:57.230Z"
      }'
    

背景函式

您可以使用 curl 將範例事件傳送至背景函式。以下 curl 要求顯示如何將 Cloud Pub/SubCloud Storage 事件範例傳送至在 localhost:8080 執行的背景函式。

Pub/Sub

# 'world' base64-encoded is 'd29ybGQ='
curl localhost:8080 \
  -X POST \
  -H "Content-Type: application/json" \
  -d '{
        "context": {
          "eventId":"1144231683168617",
          "timestamp":"2020-05-06T07:33:34.556Z",
          "eventType":"google.pubsub.topic.publish",
          "resource":{
            "service":"pubsub.googleapis.com",
            "name":"projects/sample-project/topics/gcf-test",
            "type":"type.googleapis.com/google.pubsub.v1.PubsubMessage"
          }
        },
        "data": {
          "@type": "type.googleapis.com/google.pubsub.v1.PubsubMessage",
          "attributes": {
             "attr1":"attr1-value"
          },
          "data": "d29ybGQ="
        }
      }'
    

儲存空間

curl localhost:8080 \
  -X POST \
  -H "Content-Type: application/json" \
  -d '{
        "context": {
          "eventId": "1147091835525187",
          "timestamp": "2020-04-23T07:38:57.772Z",
          "eventType": "google.storage.object.finalize",
          "resource": {
             "service": "storage.googleapis.com",
             "name": "projects/_/buckets/MY_BUCKET/MY_FILE.txt",
             "type": "storage#object"
          }
        },
        "data": {
          "bucket": "MY_BUCKET",
          "contentType": "text/plain",
          "kind": "storage#object",
          "md5Hash": "...",
          "metageneration": "1",
          "name": "MY_FILE.txt",
          "size": "352",
          "storageClass": "MULTI_REGIONAL",
          "timeCreated": "2020-04-23T07:38:57.230Z",
          "timeStorageClassUpdated": "2020-04-23T07:38:57.230Z",
          "updated": "2020-04-23T07:38:57.230Z"
        }
      }'