繁体   English   中英

如何在由谷歌云存储完成事件触发的 python 脚本中运行 bash 脚本?

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

我想在将新文件添加到谷歌云存储桶时运行 bash 脚本。 现在我可以在将文件添加到 gcs 时运行 python 脚本。 我尝试从 python 脚本中调用 bash 脚本,但在 shell 或我的日志中没有看到任何输出。

这是我订阅 gcs finalize 事件的函数 main.py

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))

这是 alert.sh bash 脚本

echo "FILE ADDED"

每次将新文件添加到 Cloud Storage 时,都会触发 Google Cloud 功能。 您不能从云函数调用 bash 脚本。 如果您想在每次添加文件或触发cloud function default logging一条消息,您可以使用cloud function default loggingStackdriver Logging Client Library

默认情况下,Cloud Functions 包括简单的日志记录。 写入 stdout 或 stderr 的日志会自动显示在 Cloud Console 中。 如需更高级的日志记录,请使用 Stackdriver Logging Client Library。

注意:在创建日志条目和它们显示您之间通常会有轻微的延迟

编写、查看和响应日志

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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