Home > Software design >  Access google storage client using dictionary
Access google storage client using dictionary

Time:11-18

I have a service account in a form of dictionary. Below is the service account

service_account = {
  "type": "service_account",
  "project_id": "project_id",
  "private_key_id": "private_key_id",
  "private_key":  "PRIVATE KEY",
  "client_email": "email",
  "client_id": "111111",
  "auth_uri": "https://auth.com",
  "token_uri": "https://token.com",
  "auth_provider_x509_cert_url": "https://certs.com",
  "client_x509_cert_url": "https://www.cert.com"
}

The above details are simualated. I want to access to the google storage using the above dictionary but not using ".json" file. Below is the code that I am trying

from google.cloud import storage
storage_client = storage.Client.from_service_account_json(service_account)
bucket = storage_client.bucket(bucket_name)
blob = bucket.blob(blob_name)
file_data = json.loads(blob.download_as_string())

Getting the below error

    storage_client = storage.Client.from_service_account_json(service_account)
  File "/usr/local/lib/python3.9/site-packages/google/cloud/client.py", line 106, in from_service_account_json
    with io.open(json_credentials_path, "r", encoding="utf-8") as json_fi:
TypeError: expected str, bytes or os.PathLike object, not dict

CodePudding user response:

It looks like this can be done with the from_service_account_info() method instead of from_service_account_json.

  • Related