How to test aGoogle Cloud Function locally when it uses google cloud storage

285 Views Asked by At

I have a cloud function I am testing locally with functions_framework.

This works fine if there are no requirements form other connected service, but how can I read file from cloud storage in my function when developing locally.

Here is a quick example of a local function run with

functions_framework --target=file_trigger

 from google.cloud import storage

 # Main entry point for the cloud function
 def file_trigger(request):
      event = {
           'bucket': 'my-cloud-bucket',
           'name': 'my-bucket-file.csv'
      }

      bucketName = event['bucket']
      blobName = event['name']

      storage_client = storage.Client()
      bucket = storage_client.bucket(bucket_name)
      blob = bucket.blob(blob_name)

      with blob.open("r") as f:
          return f.read()

Error thrown:

google.auth.exceptions.RefreshError: ('invalid_grant: Token has been expired or revoked.', {'error': 'invalid_grant', 'error_description': 'Token has been expired or revoked.'})

1

There are 1 best solutions below

0
On BEST ANSWER

Posting this as a community wiki:

Error encountered:

google.auth.exceptions.RefreshError: ('invalid_grant: Token has been expired or revoked.', {'error': 'invalid_grant', 'error_description': 'Token has been expired or revoked.'})

As stated by @JohnHanley:

The error will occur when not logged in properly on your local environment.

To solve, run this command:

gcloud auth application-default login