gcp api 部屬及建立範例

範例的git 連結
https://github.com/GoogleCloudPlatform/endpoints-quickstart
sh 內容眾多部貼上來,可以進google 的範例git 參考

1
./deploy_api.sh
  • deploy_api.sh內容
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    set -euo pipefail

    source util.sh

    main() {
    # Get our working project, or exit if it's not set.
    local project_id=$(get_project_id)
    if [[ -z "$project_id" ]]; then
    exit 1
    fi
    local temp_file=$(mktemp)
    export TEMP_FILE="${temp_file}.yaml"
    mv "$temp_file" "$TEMP_FILE"
    # Because the included API is a template, we have to do some string
    # substitution before we can deploy it. Sed does this nicely.
    < "$API_FILE" sed -E "s/YOUR-PROJECT-ID/${project_id}/g" > "$TEMP_FILE"
    echo "Deploying $API_FILE..."
    echo "gcloud endpoints services deploy $API_FILE"
    gcloud endpoints services deploy "$TEMP_FILE"
    }

    cleanup() {
    rm "$TEMP_FILE"
    }

    # Defaults.
    API_FILE="../openapi.yaml"

    if [[ "$#" == 0 ]]; then
    : # Use defaults.
    elif [[ "$#" == 1 ]]; then
    API_FILE="$1"
    else
    echo "Wrong number of arguments specified."
    echo "Usage: deploy_api.sh [api-file]"
    exit 1
    fi

    # Cleanup our temporary files even if our deployment fails.
    trap cleanup EXIT

    main "$@"
  • output
    1
    Service Configuration [2017-02-13-r2] uploaded for service [airports-api.endpoints.example-project.cloud.goog]

啟動api 後端

1
./deploy_app.sh

測試 request

1
./query_api.sh
1
./query_api.sh JFK

追蹤api 活動

1
./generate_traffic.sh

加上 api quota

1
2
./deploy_api.sh ../openapi_with_ratelimit.yaml
./deploy_app.sh

api key

先進入Navigation menu > APIs & Services > Credentials.,創建api key

1
export API_KEY=YOUR-API-KEY
1
./query_api_with_key.sh $API_KEY

創建後測試

1
./generate_traffic_with_key.sh $API_KEY
1
./query_api_with_key.sh $API_KEY

gcp api 部屬及建立範例
https://shengshengyang.github.io/2023/12/09/gcp-api-usage/
作者
Dean Yang
發布於
2023年12月9日
許可協議