title: Read/Write Google Sheets from within a Cloud Function
body: <p>I am trying to call the Sheets API from within a google cloud function in order to read from Google Sheets and Write to Google Sheets.  My credentials to access are stored within a google storage bucket called <code>creds_bucket</code> in a file called <code>my_creds.json</code>.  The two spreadsheets I'm using both have the 'my_creds' email linked to them and all the relevant APIs have bee enabled for project.</p>
  
  <p>Here is my code....  I get a KEY error that says it doesn't recognise the <code>creds_bucket</code>  .  What would be driving this?</p>
  
  <pre><code>import httplib2
  from apiclient.discovery import build
  from oauth2client.service_account import ServiceAccountCredentials
  import os
  import json
  from google.cloud import storage
  
  def main(data, context):
  
      blob = storage.Client().get_bucket(os.environ['creds_bucket']).get_blob('my_creds.json').download_as_string()
  
      parsed_json_creds = json.loads(blob)
  
      scope = ['https://www.googleapis.com/auth/drive','https://www.googleapis.com/auth/spreadsheets']
      credentials = ServiceAccountCredentials.from_json_keyfile_name(parsed_json_creds, scope)
  
      service = build('sheets', 'v4', http=credentials.authorize(httplib2.Http()))
  
      spreadsheet_id_input = 'spreadsheetinput_ID'
      range_input = 'Sheet1!A1:D10'
  
      spreadsheet_id_ouput = 'SPREADHEET_Output_ID'
      range_ouput = 'Sheet1!A1:D10'
  
      # pull data from input google sheet
      response = service.spreadsheets().values().get(spreadsheetId=spreadsheet_id_input, range=range_input).execute()
      # export pulled data from input sheet to output sheet
      request2 = service.spreadsheets().values().update(spreadsheetId=spreadsheet_id_ouput, range=range_ouput, body=response)
      response2 = request2.execute()
  </code></pre>
  
  <pre><code>  File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker.py", line 383, in run_background_function
      _function_handler.invoke_user_function(event_object)
    File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker.py", line 217, in invoke_user_function
      return call_user_function(request_or_event)
    File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker.py", line 214, in call_user_function
      event_context.Context(**request_or_event.context))
    File "/user_code/main.py", line 10, in main
      blob = storage.Client().get_bucket(os.environ['creds_bucket']).get_blob('my_creds.json').download_as_string()
    File "/env/lib/python3.7/os.py", line 678, in __getitem__
      raise KeyError(key) from None
  KeyError: 'creds_bucket'
  </code></pre>