问题
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)
回答1:
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.
来源:https://stackoverflow.com/questions/57101015/how-to-connect-to-a-firestore-database-with-ruby