【问题标题】:Gcloud functions deploy with specific python file and specific function nameGcloud 函数使用特定的 python 文件和特定的函数名称部署
【发布时间】:2021-05-09 06:37:56
【问题描述】:

我正在尝试部署几个 Cloud Functions,它们都在同一个存储库中,但在不同的 python 文件中。

基本上,我有这两个功能

my_repo
- function_1_folder
  - function_1.py
  - requirements.txt
  - .gcloudignore
- function_2_folder  
  - function_2.py
  - requirements.txt
  - .gcloudignore

function_1.py 内部,我有function_1(),我想将它部署到我的云函数function1(注意这里没有下划线),function_2 也是如此。

我转到 function_1_folder 并指定了一个入口点 (--entry-point function_1),但出现“缺少 main.py”错误。

如何同时指定 python 文件名和函数名(如果可能)? gcloud 是否还会部署安装我的功能所依赖的软件包所需的requirements.txt

谢谢

【问题讨论】:

    标签: python google-cloud-platform google-cloud-functions gcloud


    【解决方案1】:

    您不能随意命名源文件。这是structuring requirements 的一部分,您的Python 源文件应始终命名为main.py。你的目录应该是这样的:

    my_repo
     - function_1_folder
       - main.py
       - requirements.txt
       - .gcloudignore
     - function_2_folder  
       - main.py
       - requirements.txt
       - .gcloudignore
    

    标志--entry-point 用于指定源文件中函数的名称。例如(作为 HTTP):

    ma​​in.py

    def function_1(request):
        return 'Hello World'
    

    function_1_folder中运行这个命令:

    gcloud functions deploy function1 --entry-point function_1 --runtime python37 --trigger-http 
    

    为了回答您的最后一个问题,requirements.txt 与您的 main.py 一起包含,并且是有效的配置。这些依赖项将在构建期间安装。

    作为附加参考,请参阅:https://github.com/GoogleCloudPlatform/python-docs-samples/tree/master/functions

    【讨论】:

    • 除了'main.py'之外,你还可以在同一个目录中拥有任意数量的其他python文件(即'file1.py'、'file2,py'等)与'main.py'。这些模块可以以普通方式导入(并在其中使用)“main.py”。除此之外,还可以使用该目录中的任何其他文件(即 json)。这些文件可以作为普通文件从代码内部读取(和使用)。
    猜你喜欢
    • 2014-11-27
    • 1970-01-01
    • 1970-01-01
    • 2023-01-24
    • 1970-01-01
    • 2019-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多