简体   繁体   中英

How do I run a bash script inside python script triggered by google cloud storage finalize event?

I want to run a bash script when a new file is added to a google cloud storage bucket. Right now I can run a python script when a file is added to gcs. I tried calling a bash script from within the python script but don't see any output in the shell or in my logs.

Here is my function main.py that is subscribed to the gcs finalize event

import subprocess
# [START functions_helloworld_storage_generic]                                                                                                                                                                                          
def hello_gcs_generic(data, context):
    subprocess.call("alert.sh", shell=True)
    """Background Cloud Function to be triggered by Cloud Storage.                                                                                                                                                                      
       This generic function logs relevant data when a file is changed.                                                                                                                                                                 

    Args:                                                                                                                                                                                                                               
        data (dict): The Cloud Functions event payload.                                                                                                                                                                                 
        context (google.cloud.functions.Context): Metadata of triggering event.                                                                                                                                                         
    Returns:                                                                                                                                                                                                                            
        None; the output is written to Stackdriver Logging                                                                                                                                                                              
    """
    print('Event ID: {}'.format(context.event_id))
    print('Event type: {}'.format(context.event_type))

Here is the alert.sh bash script

echo "FILE ADDED"

The Google Cloud function will be triggered every time a new file will be added to Cloud Storage. You can not call bash scripts from your cloud function. If you want to logg a message every time a file is added, or the function was triggered, you can use cloud function default logging or Stackdriver Logging Client Library .

Cloud Functions includes simple logging by default. Logs written to stdout or stderr will appear automatically in the Cloud Console. For more advanced logging, use the Stackdriver Logging Client Library.

Note: There is typically a slight delay between when log entries are created and when they show u

Writing, Viewing, and Responding to Logs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM