Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deploy google function from google SDK from local system runtime python

I am trying to deploy Google Cloud Function from local system using the gcloud command-line tool.

My files are compressed in a .zip file. As described in the attached screenshot the zip file contains main.py and requirement.txt

main.py (took from web for testing)

from wand.image import Image
from google.cloud import storage
client = storage.Client.from_service_account_json(
         'C:\\Users\\an20086686\\AppData\\Local\\Google\\Cloud SDK\\deploy\\****-*****-poc-e4b410a65986.JSON')
THUMBNAIL_BUCKET = 'a_demo_storage_trigger1'
def make_thumbnail(data, context):

    bucket = client.get_bucket(data['****_my-awesome-bucket'])
    blob = bucket.get_blob(data['name'])
    imagedata = blob.download_as_string()

    newimage = Image(blob=imagedata)
    newimage.sample(200,200)

    bucket = client.get_bucket(THUMBNAIL_BUCKET)
    newblob = bucket.blob('thumbnail-' + data['name'])     
    newblob.upload_from_string(newimage.make_blob())

requirements.txt

google-cloud-storage
Wand

Using the command below to deploy this function:

gcloud beta functions deploy make_thumbnail --runtime python37 \
--trigger-bucket ****_my-awesome-bucket \
--trigger-event google.storage.object.finalize

Error 1 : (gcloud.beta.functions.deploy) unrecognized arguments: google.storage.object.finalize

When I use:

gcloud beta functions deploy make_thumbnail --runtime python37 \
--trigger-bucket ****_my-awesome-bucket \
--trigger-event google.storage.object.finalize

ERROR: (gcloud.beta.functions.deploy) OperationError: code=3,message=Function failed on loading user code.

Error message: File main.py that is expected to define function doesn't exist

Log of the encountered error:

Screenshot

like image 695
Ravi Kumar Avatar asked Apr 12 '26 22:04

Ravi Kumar


2 Answers

The tutorial doesn't use a ZIP file.

If you want to use a ZIP file, you'd need to specify it with the --source option. See docs.

And not sure if this is a typo in your question, but there needs to be a space before --trigger-event.

like image 147
YaguraStation Avatar answered Apr 14 '26 13:04

YaguraStation


As @YaguraStation mentioned the issue is because of the ZIP file. Let me give a bit more of an explanation though.

If you are following this documentation,Deploying from Your Local Machine, in order to deploy your function with the gcloud command your files should not be compressed in a ZIP file.

Your command somehow should look like this:

gcloud functions deploy make_thumbnail --runtime python37 \
--trigger-resource ****_my-awesome-bucket \
--trigger-event google.storage.object.finalize

Note: RUNTIME and TRIGGER are two different values.

If you want to deploy a function and your files to be compressed into a ZIP file then add the --source flag:

gcloud functions deploy make_thumbnail --runtime python37 \
--trigger-resource ****_my-awesome-bucket \
--trigger-event google.storage.object.finalize \
--source gs://path/to_file/file.zip

I tested both of the commands above and they are working for me as intended.

like image 23
tzovourn Avatar answered Apr 14 '26 14:04

tzovourn