LoginSignup
2
0

More than 3 years have passed since last update.

curl で Cloud Firestore のデータを作成

Last updated at Posted at 2020-01-29

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"
#
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0