How to read files outside main.go, and make it work on AWS lambda

我的未来我决定 提交于 2021-01-29 07:15:10

问题


On AWS Lambda, I want to use CSV file outside main.go

Of course this works when I follow these steps.

  1. cd ~/go/src/lambda-go-sample
  2. GOOS=linux go build main.go
  3. zip main.zip main
  4. upload to Lambda and test => "Hello ƛ!"

main.go

package main

import (
    "github.com/aws/aws-lambda-go/lambda"
)

func hello() (string, error) {
    return "Hello ƛ!", nil
}

func main() {
    lambda.Start(hello)
}

Now I add sample.csv on the same directory of main.go ,and add code to read it.

lambda-go-sample

|- main.go

|- sample.csv

main.go

...

func hello()([]string ,error){
   queries, err := readCSV("sample.csv")
   return queries, err
}

func main(){
   lambda.Start(hello)
}

func readCSV(fileName string) (queries []string, err error) {
    f, err := os.Open(fileName)
    if err != nil {
        fmt.Fprintln(os.Stderr, "Failed to read CSV file", err)
        return nil, err
    }

    r := csv.NewReader(f)
    for {
        record, err := r.Read()
        if err == io.EOF {
            break
        }
        if err != nil {
            fmt.Fprintln(os.Stderr, "Failed to read CSV record", err)
            return nil, err
        }

        companyName := strings.Replace(record[1], "INC", "", -1)
        queries = append(queries, companyName+"CM")
    }
    return queries, nil
}

Then, when I follow the same steps as above, this error occurs on console.

{
  "errorMessage": "open sample: no such file or directory",
  "errorType": "PathError"
}

How can I solve this problem??


回答1:


I have no idea whether that "lambda" thing has this concept, but when a process works in a contemporary commodity OS it has the concept of "current directory" or "working directory" or "current working directory"; any attempt to access a file using a relative (not absolute) name will have that name interpreted as relative to the CWD of the current process.

Note two further things:

  • When launching the process, the OS is free to set its CWD to whatever value it wants.
    It may not be immediately obvious for when you run a process in an interactive shell, the shell sets the CWD of the process to the directory which is current for the shell, and the OS itself does not interfere.
  • The process can change its CWD at runtime.

Note one more thing: that "lambda" thing might actually have a concept similar but orthogonal to the CWD: for instance, it might require the process to query some environment variable (or other information resource) to know where its "assets" are placed.

So, to step back a little bit. You might start with os.Getwd to get the process' CWD and then use path/filepath.Join to obtain the full name of your CSV file and then attempt to read it.

If that fails, you might want to dig deeper on what runtime environment that "lambda" thing actually provides to the processes it runs.
If I were you, I'd start with this bit.



来源:https://stackoverflow.com/questions/64241046/how-to-read-files-outside-main-go-and-make-it-work-on-aws-lambda

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!