Skip to content

Issues with @storage_fn.on_object_finalized() and Firebase Emulator #196

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mnsRG opened this issue Apr 16, 2024 · 3 comments
Closed

Issues with @storage_fn.on_object_finalized() and Firebase Emulator #196

mnsRG opened this issue Apr 16, 2024 · 3 comments
Labels
waiting for customer response Issue is on hold awaiting more information from OP

Comments

@mnsRG
Copy link

mnsRG commented Apr 16, 2024

Hello, I ‘ve been trying to test a locally deployed google cloud function that looks like this:

@storage_fn.on_object_finalized(bucket=MY_BUCKET)
def finalized_bucket(
    cloud_event: storage_fn.CloudEvent[storage_fn.StorageObjectData]
) -> None:

    print("triggered!")
    return "ok"

My gcloud command to deploy the function is this one:


gcloud alpha functions local deploy example \
        --gen2 \
        --entry-point=finalized_bucket \
        --runtime=python312 \
	--port 8083 \
        --env-vars-file="../../.env.local.yml"

The function is deployed successfully and then I am testing it with:

curl localhost:8083 \
  -X POST \
  -H "Content-Type: application/json" \
  -d '{
        "context": {
          "eventId": "1147091835525187",
          "timestamp": "2020-04-23T07:38:57.772Z",
          "eventType": "google.storage.object.finalize",
          "resource": {
             "service": "storage.googleapis.com",
             "name": "projects/_/buckets/MY_BUCKET/MY_FILE.txt",
             "type": "storage#object"
          }
        },
        "data": {
          "bucket": "MY_BUCKET",
          "contentType": "text/plain",
          "kind": "storage#object",
          "md5Hash": "...",
          "metageneration": "1",
          "name": "MY_FILE.txt",
          "size": "352",
          "storageClass": "MULTI_REGIONAL",
          "timeCreated": "2020-04-23T07:38:57.230Z",
          "timeStorageClassUpdated": "2020-04-23T07:38:57.230Z",
          "updated": "2020-04-23T07:38:57.230Z"
        }
      }'

Then, I get the following error (from the logs):

2024-04-17 00:12:40 [2024-04-16 22:12:40,786] ERROR in app: Exception on / [POST]
2024-04-17 00:12:40 Traceback (most recent call last):
2024-04-17 00:12:40   File "/layers/google.python.pip/pip/lib/python3.12/site-packages/flask/app.py", line 1473, in wsgi_app
2024-04-17 00:12:40     response = self.full_dispatch_request()
2024-04-17 00:12:40                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-04-17 00:12:40   File "/layers/google.python.pip/pip/lib/python3.12/site-packages/flask/app.py", line 882, in full_dispatch_request
2024-04-17 00:12:40     rv = self.handle_user_exception(e)
2024-04-17 00:12:40          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-04-17 00:12:40   File "/layers/google.python.pip/pip/lib/python3.12/site-packages/flask/app.py", line 880, in full_dispatch_request
2024-04-17 00:12:40     rv = self.dispatch_request()
2024-04-17 00:12:40          ^^^^^^^^^^^^^^^^^^^^^^^
2024-04-17 00:12:40   File "/layers/google.python.pip/pip/lib/python3.12/site-packages/flask/app.py", line 865, in dispatch_request
2024-04-17 00:12:40     return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)  # type: ignore[no-any-return]
2024-04-17 00:12:40            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-04-17 00:12:40   File "/layers/google.python.pip/pip/lib/python3.12/site-packages/functions_framework/__init__.py", line 134, in view_func
2024-04-17 00:12:40     return function(request._get_current_object())
2024-04-17 00:12:40            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-04-17 00:12:40   File "/layers/google.python.pip/pip/lib/python3.12/site-packages/firebase_functions/storage_fn.py", line 332, in on_object_finalized_wrapped
2024-04-17 00:12:40     return _message_handler(func, raw)
2024-04-17 00:12:40            ^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-04-17 00:12:40   File "/layers/google.python.pip/pip/lib/python3.12/site-packages/firebase_functions/storage_fn.py", line 206, in _message_handler
2024-04-17 00:12:40     event_attributes = raw._get_attributes()
2024-04-17 00:12:40                        ^^^^^^^^^^^^^^^^^^^
2024-04-17 00:12:40 AttributeError: 'Request' object has no attribute '_get_attributes'. Did you mean: '__getattribute__'?

Additionally, when I test its actual functionality (do something when an object is added to the bucket) there is nothing happening (no event triggered). The storage bucket I am using is running on firebase emulator. I am using the ‘STORAGE_EMULATOR_HOST’ environment variable and I initialise the app with the emulator-adapted project id. The deployed function can read the bucket’s contents (I can print a list of the blobs when I spin it up), but the function is not triggered when I am adding a new file, meaning that I am not receiving the above error either.

I tried with different versions of firebase-functions: 0.4.0, 0.3.0, 0.2.0 and I still get the same behaviour.

Any help would be appreciated.
Thanks

@exaby73
Copy link
Contributor

exaby73 commented Apr 18, 2024

Hey @mnsRG. At first glance, it seems to be an issue with the payload you're sending in the cURL request. Basically, there are some attributes missing there. Could you point me to where you found the JSON structure you're using?

@exaby73 exaby73 added the waiting for customer response Issue is on hold awaiting more information from OP label Apr 18, 2024
@mnsRG
Copy link
Author

mnsRG commented Apr 18, 2024

Hi @exaby73 . I got it from this link: google cloud functions

In the meantime I rewrote the function using Node.js as a firebase function and tested it with this request:

curl -X POST localhost:8083 \
   -H "Content-Type: application/cloudevents+json" \
   -d '{                               
        "specversion" : "1.0",
        "type" : "example.com.cloud.event",
        "source" : "https://example.com/cloudevents/pull",
        "subject" : "123",                        
        "id" : "A234-1234-1234",                        
        "time" : "2018-04-05T17:31:00Z",
        "data" : {}
}'  

It worked well. Also adding a file to the emulator storage successfully triggered the function.

So, probably doing the same request to the python function would also work (have to test it though). Also if I get it right, I should not deploy the python function using gcloud alpha functions local but use it internally in firebase (like I did with Node.js).

Therefore it could also be that the issue is more related to how google cloud functions and firebase functions in the same project work together (especially in an emulator setup). Which means that you can probably close this issue 🙂

@exaby73
Copy link
Contributor

exaby73 commented Apr 18, 2024

Okay understood. I'll close this issue but if you do face this again, feel free to reopen this issue, or open a new one if you can't and mention this issue in the new one. Thank you for trying out Python Functions :)

@exaby73 exaby73 closed this as not planned Won't fix, can't repro, duplicate, stale Apr 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
waiting for customer response Issue is on hold awaiting more information from OP
Projects
None yet
Development

No branches or pull requests

2 participants