使用Go 1.13在Google Cloud Function上“没有此类文件或目录”

问题描述

我有一个用Go编写的Google Cloud Function,并使用Go 1.11运行时进行了部署,并且可以运行。将运行库升级到Go 1.13,具有相同的功能时出现问题,并且出现此错误

Schema::create('statistics ',function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->bigInteger('user_id')->unsigned();

            $table->foreign('user_id')
                ->references('id')
                ->on('users')
                ->onDelete('cascade');
        }); 

在Cloud Function的日志中,我有以下错误的详细信息:

Error: Could not handle the request

文件与Go文件位于同一文件夹中。

以下是该函数的相关代码

Function execution started
open ./progress.html: no such file or directory
Function execution took 232 ms,finished with status: 'connection error'

如果有帮助,请here is the repository使用此功能

唯一更改的是用于部署它的Go运行时,从1.11到1.13。

以下是用于部署它的两个命令:

有效:

// Progress ... Entrypoint of our Cloud Function
func Progress(w http.ResponseWriter,r *http.Request) {
        
        ...

        tpl,err := template.ParseFiles("progress.html")
        if err != nil {
            log.Fatalln(err)
        }

        buf := new(bytes.Buffer)

        err = tpl.Execute(buf,data)
        if err != nil {
            log.Fatalln(err)
        }
       ...
    }

它不起作用(成功部署,但是使用该功能时出现错误):

gcloud functions deploy progress --runtime go111 --entry-point Progress --trigger-http --memory 128MB

解决方法

该错误是假设使用不同运行时的Google Cloud Functions实现的是相同的,但是调试后,我发现了一些区别。

这是一个调试它的测试功能:

func Progress(w http.ResponseWriter,r *http.Request) {
    wd,_ := os.Getwd()
    fmt.Fprintf(w,"$> pwd\n%s\n\n",wd)
  
    bytes,_ = exec.Command("ls","-l").CombinedOutput()
    fmt.Fprintf(w,"$> ls -l\n%s\n\n",bytes)
}

以下是使用Go 1.11运行时的输出:

$> pwd
/srv/files/

$> ls -l
total 5
-rw-r--r-- 1 root root 1068 Aug 13 04:15 LICENSE
-rw-r--r-- 1 root root 1097 Aug 13 04:15 README.md
drwxr-xr-x 2 root root    0 Aug 13 04:15 colors-example
-rw-r--r-- 1 root root 2093 Aug 13 04:15 progress.go
-rw-r--r-- 1 root root  627 Aug 13 04:15 progress.html

如您所见,所有文件都在当前目录中,包括丢失的文件progress.html

但这是使用Go 1.13运行时的输出:

$> pwd
/srv

$> ls -l
total 0
drwxr-xr-x 2 www-data www-data 0 Jan  1  1980 pkg
drwxr-xr-x 2 www-data www-data 0 Jan  1  1980 src

请注意我的文件不再存在,因此我打印了src的内容,并包含一个名为progress(项目名称)的文件夹。

在那个文件夹里,有我所有的文件。

因此,使用Go 1.13运行时的解决方法是:

// Progress ... Entrypoint of our Cloud Function
func Progress(w http.ResponseWriter,r *http.Request) {
        
        ...

        tpl,err := template.ParseFiles("src/progress/progress.html")
        if err != nil {
            log.Fatalln(err)
        }

        buf := new(bytes.Buffer)

        err = tpl.Execute(buf,data)
        if err != nil {
            log.Fatalln(err)
        }
       ...
    }

我希望它不仅对答案有帮助,而且对于新的运行时规则有所更改,您都可以进行调试并找到文件的正确位置。