dsam70528
2019-01-21 15:43
浏览 42
已采纳

如何在GCP云功能内加载本地资产?

I'm building a tiny GCP cloud function in Golang that returns a generated PNG file when calling it via HTTP. I deployed my code via ZIP-Upload in the Google Cloud Console. Currently it gets called properly and the code gets compiled etc. But in my code I have to load several local files - a font and a .png image. I bundled those in the ZIP I uploaded and the files are visible in the Source-View in GCP. All files (images, fonts and go-files) are within the same directory.

When calling the cloud function the log states the following:

2019/01/21 14:59:31 open /english.png: no such file or directory

I tried to change the way i build the path to the file in go. I already used /german.png statically, used several attempts to build the path dynamically.

I'm not 100 percent sure if this is the way to go, but it is my first experiment with "serverless" and i am willing to get it done the "right" way.

import "github.com/fogleman/gg"


func main() {
    ex, err := os.Executable()
    if err != nil {
        panic(err)
    }
    executableDir := filepath.Dir(ex)

    img, err :=gg.LoadPNG(path.Join(executableDir, "./english.png"))
    if err != nil {
        log.Fatal(err)
    }
}

Currently the file can not be found in any attempt i made. Maybe the path the images get "deployed" into are different from the ones i tried - i have not found any note on that in the documentation. I'd obviously expect it to be loaded properly.

图片转代码服务由CSDN问答提供 功能建议

我正在Golang中构建一个小的GCP云函数,当通过HTTP调用生成的PNG文件时,该函数会返回生成的PNG文件。 我在Google Cloud Console中通过ZIP-Upload部署了我的代码。 当前它已被正确调用,并且代码已被编译等。但是在我的代码中,我必须加载多个本地文件-字体和.png图像。 我捆绑了 我上传的ZIP中的文件和文件在GCP的“源视图”中可见。 所有文件(图像,字体和转到文件)都在同一目录中。

调用云函数时,日志记录以下内容:

   2019/01/21 14:59:31打开/english.png:没有这样的文件或目录
   
 
 

我试图更改构建文件的方式 转到文件的路径。 我已经静态地使用过/german.png,进行了几次尝试来动态构建路径。

我不确定100%是否可行,但这是我的第一个实验

  import“ github.com/fogleman/gg"


func main(  ){
 ex,err:= os.Executable()
如果err!= nil {
 panic(err)
} 
 executeDir:= filepath.Dir(ex)
 
 img,err:  = gg.LoadPNG(path.Join(executableDir,“ ./english.png"))
 if err!= nil {
 log.Fatal(err)
} 
} 
   
 
 

当前我进行任何尝试都找不到该文件。 也许图像“部署”到的路径与我尝试过的路径不同-我在文档中没有找到任何说明。 我显然希望图像能够正确加载。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doudong1117 2019-01-21 16:40
    已采纳

    I created http functions with the following structure:

    api
    |--test.txt
    |--api.go
    

    And wrote simple function to reply with file content:

    package api
    
    import (
        "io/ioutil"
        "net/http"
    )
    
    // FileTest func
    func FileTest(w http.ResponseWriter, r *http.Request) {
        content, err := ioutil.ReadFile("./test.txt")
        if err != nil {
            w.WriteHeader(http.StatusInternalServerError)
            w.Write([]byte(err.Error()))
            return
        }
    
        w.Write(content)
    }
    

    It returns the file content without any problems. https://us-central1-clickshield24m.cloudfunctions.net/api

    So in your case I would try change the path to gg.LoadPNG("./english.png")

    打赏 评论

相关推荐 更多相似问题