如何在main.go之外读取文件,并使其在AWS Lambda上运行 [英] How to read files outside main.go, and make it work on AWS lambda

查看:78
本文介绍了如何在main.go之外读取文件,并使其在AWS Lambda上运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在AWS Lambda上,我想在main.go之外使用CSV文件

当我按照以下步骤操作时,这当然可以工作.

  1. cd〜/go/src/lambda-go-sample
  2. GOOS = linux可以构建main.go
  3. zip main.zip main
  4. 上传到Lambda并测试=>你好ƛ!"

main.go

 程序包主要进口 (" github.com/aws/aws-lambda-go/lambda)func hello()(字符串,错误){返回"Helloƛ!",为零}func main(){lambda.Start(你好)} 

现在,我在main.go的同一目录中添加sample.csv,并添加代码以读取它.

lambda-go-sample

|-main.go

|-sample.csv

main.go

  ...func hello()([] string,error){查询,错误:= readCSV("sample.csv")返回查询,错误}func main(){lambda.Start(你好)}func readCSV(fileName字符串)(查询[]字符串,错误错误){f,err:= os.Open(文件名)如果err!= nil {fmt.Fprintln(os.Stderr,无法读取CSV文件",err)返回nil,err}r:= csv.NewReader(f)为了 {记录err:= r.Read()如果err == io.EOF {休息}如果err!= nil {fmt.Fprintln(os.Stderr,无法读取CSV记录",err)返回nil,err}companyName:=字符串.替换(记录[1],"INC",",-1)查询=追加(查询,companyName +"CM")}返回查询,无} 

然后,当我执行与上述相同的步骤时,在控制台上会发生此错误.

  {"errorMessage":打开示例:没有这样的文件或目录"," errorType":" PathError"} 

我该如何解决这个问题?

解决方案

我不知道该"lambda"是否事物具有这个概念,但是当流程在现代商品OS中运行时,它具有当前目录"的概念;或工作目录"或当前工作目录" ;任何使用相对(非绝对)名称访问文件的尝试都会将该名称解释为相对于当前进程的CWD.

请注意另外两件事:

  • 启动该进程时,操作系统可以自由将其CWD设置为所需的任何值.
    当您在交互式外壳程序中运行进程时,外壳程序将进程的CWD设置为该外壳程序的当前目录,并且操作系统本身不会产生干扰,这可能不会立即显而易见.
  • 该进程可以在运行时更改其CWD.

再注意一件事:"lambda"事物实际上可能具有与CWD类似但正交的概念:例如,它可能需要查询某些环境变量(或其他信息资源)以知道其资产"在何处的过程.被放置.

所以,要退后一点.您可以从 os.Getwd 开始以获取流程"CWD,然后使用 path/filepath.Join 来获取您的CSV文件的全名,然后尝试读取它.

如果失败,则您可能想更深入地了解"lambda"在哪个运行时环境中运行.事物实际上提供了它运行的流程.
如果我是你,我会开始.

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

解决方案

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.

这篇关于如何在main.go之外读取文件,并使其在AWS Lambda上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆