curl で Cloud Firestore のデータを取得


Cloud Firestore の RestAPI を使ってデータを取得します。

1) project-aaa の cities の t0921 を取得

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'
#
#
curl -H "Authorization: Bearer $ACCESS_TOKEN" $URL"/t0921"

実行結果

{
  "name": "projects/project-jan25-2020/databases/(default)/documents/cities/t0921",
  "fields": {
    "population": {
      "integerValue": "81235"
    },
    "name": {
      "stringValue": "宇都宮"
    },
    "date_mod": {
      "stringValue": "1950-9-24"
    }
  },
  "createTime": "2020-01-25T06:14:38.983529Z",
  "updateTime": "2020-01-25T06:23:16.019805Z"
}

2) Collection 内の全てのデータを取得

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'
#
curl -H "Authorization: Bearer $ACCESS_TOKEN" $URL