简体   繁体   中英

How to connect to a Firestore database with Ruby

I'm trying to write and read to and from my Firestore database from a Ruby script, eventually from a Rails application.

It seems like a trivial task but when I run a test script inside IRB it won't let me write or read data.

I tried:

  • successfully installed google-cloud-firestore gem
  • downloaded JSON keyfile from within the Firestore console.
  • security rules are set to public (read and write)
require 'google/cloud/firestore' # => true

# credentials
cred = Google::Cloud::Firestore::Credentials.new('<path/to/my/keyfile/key-projectname-etc.json>') 
# => #<Google::Cloud::Firestore::Credentials:...>

# initialize firestore connection
db = Google::Cloud::Firestore.new(
  project_id: '<MY-PROJECT-ID>',
  credentials: cred
) # => #<Google::Cloud::Firestore::Client:...>

# write a document inside the users collection
db.doc("users/helloFromRuby").set({ name: "yay it worked!"})

This is the error message I get:

Google::Cloud::UnavailableError (14:failed to connect to all addresses)

I could resolve my issue: I had an environmental variable GOOGLE_APPLICATION_CREDENTIALS initialized that was somehow interfereing with my json keyfile. Deleting that variable solved my issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM