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
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With