curl で Cloud Firestore のデータを作成


Cloud Firestore の RestAPI を使ってデータを作成します。

in01.json
{
      "fields": {
        "name": { "stringValue": "宇都宮" },
        "population": { "integerValue": "7592100" },
        "date_mod": { "stringValue": "2020-01-27" }
      }
}
in02.json
{
      "fields": {
        "name": { "stringValue": "小山" },
        "population": { "integerValue": "3592100" },
        "date_mod": { "stringValue": "2020-01-27" }
      }
}
in03.json
{
      "fields": {
        "name": { "stringValue": "佐野" },
        "population": { "integerValue": "9214600" },
        "date_mod": { "stringValue": "2020-01-27" }
      }
}

GOOGLE_APPLICATION_CREDENTIALS で定義されている project の cities に書き込み

firestore_create.sh
#
export GOOGLE_APPLICATION_CREDENTIALS="***.json"
#
ACCESS_TOKEN=`gcloud auth application-default print-access-token`
#
project_id=`jq .project_id $GOOGLE_APPLICATION_CREDENTIALS | sed "s/\"//g"`
echo $project_id
URL='https://firestore.googleapis.com/v1/projects/'$project_id'/databases/(default)/documents/cities?documentId='
#
curl -H "Authorization: Bearer $ACCESS_TOKEN" \
    -H 'Content-Type: application/json' \
     -X POST -d@in01.json $URL"t0921"
#
curl -H "Authorization: Bearer $ACCESS_TOKEN" \
    -H 'Content-Type: application/json' \
     -X POST -d@in02.json $URL"t0922"
#
curl -H "Authorization: Bearer $ACCESS_TOKEN" \
    -H 'Content-Type: application/json' \
     -X POST -d@in03.json $URL"t0923"
#