简体   繁体   English

gspread-pandas 作为 AWS Lambda 中的层

[英]gspread-pandas as layer in AWS Lambda

So I tried to set-up the package in a layer in AWS Lambda to use it later in a function.所以我尝试在 AWS Lambda 的一个层中设置包,以便稍后在函数中使用它。 Unfortunately, I can't seem to make the env variable work or set it properly so it can read the google_secret.json不幸的是,我似乎无法使 env 变量工作或正确设置它以便它可以读取 google_secret.json

To prepare the lambda package I:要准备 lambda 包,我:

  1. Created an EC2 instance创建了一个 EC2 实例
  2. Created a local directory: build/python/lib/python3.7/site-packages创建本地目录: build/python/lib/python3.7/site-packages
  3. Installed all the packages I use in that directory, including gspread_pandas安装了我在该目录中使用的所有软件包,包括 gspread_pandas
  4. Zipped the python directory压缩python目录
  5. Uploaded it to S3 and then used it as a layer上传到S3然后作为图层使用

The process worked for all the other packages except for this one, as I didn't have the config pointing to the google_secret.json该过程适用于除此之外的所有其他软件包,因为我没有指向 google_secret.json 的配置

As a fix I tried to:作为修复,我试图:

  1. Change this path in the config file _default_dir = ( Path(environ.get("$XDG_CONFIG_HOME", Path(environ.get("HOME")) / ".config")) / "gspread_pandas" ) to a path I created in the python directory (that was zipped) where I stored the google_secret.json: "python/config/gspread_pandas/"在配置文件_default_dir = ( Path(environ.get("$XDG_CONFIG_HOME", Path(environ.get("HOME")) / ".config")) / "gspread_pandas" ) 中_default_dir = ( Path(environ.get("$XDG_CONFIG_HOME", Path(environ.get("HOME")) / ".config")) / "gspread_pandas"路径更改为我在其中创建的路径我存储 google_secret.json 的 python 目录(已压缩)"python/config/gspread_pandas/"
  2. Create a file google_secret.json in the lambda function, create a environment variable "GSPREAD_PANDAS_CONFIG_DIR" and set it the file's path在 lambda 函数中创建一个文件 google_secret.json,创建一个环境变量"GSPREAD_PANDAS_CONFIG_DIR"并将其设置为文件的路径

None of the above worked, unfortunately.不幸的是,以上都没有奏效。

In both cases, I get the following error在这两种情况下,我都会收到以下错误

    { "errorMessage": "expected str, bytes or os.PathLike object, not NoneType", 
"errorType": "TypeError", 
"stackTrace": [ " File \"/var/lang/lib/python3.7/imp.py\", line 234, in load_module\n return load_source(name, filename, file)\n", 
" File \"/var/lang/lib/python3.7/imp.py\", line 171, in load_source\n module = _load(spec)\n", 
" File \"<frozen importlib._bootstrap>\", line 696, in _load\n", " File \"<frozen importlib._bootstrap>\", line 677, in _load_unlocked\n", 
" File \"<frozen importlib._bootstrap_external>\", line 728, in exec_module\n", 
" File \"<frozen importlib._bootstrap>\", line 219, in _call_with_frames_removed\n", 
" File \"/var/task/lambda_function.py\", line 8, in <module>\n import gspread_pandas\n", 
" File \"/opt/python/lib/python3.7/site-packages/gspread_pandas/__init__.py\", line 5, in <module>\n from .client import Client\n", 
" File \"/opt/python/lib/python3.7/site-packages/gspread_pandas/client.py\", line 15, in <module>\n from gspread_pandas.conf import default_scope, get_creds\n", 
" File \"/opt/python/lib/python3.7/site-packages/gspread_pandas/conf.py\", line 25, in <module>\n Path(environ.get(\"$XDG_CONFIG_HOME\", Path(environ.get(\"HOME\")) / \".config\"))\n", 
" File \"/var/lang/lib/python3.7/pathlib.py\", line 1022, in __new__\n self = cls._from_parts(args, init=False)\n", 
" File \"/var/lang/lib/python3.7/pathlib.py\", line 669, in _from_parts\n drv, root, parts = self._parse_args(args)\n", 
" File \"/var/lang/lib/python3.7/pathlib.py\", line 653, in _parse_args\n a = os.fspath(a)\n" ] }

I have to mention that I don't have much experience with coding, so I apologize in advance for the poor explanation.我不得不提一下,我没有太多的编码经验,所以我提前为糟糕的解释道歉。

Any help would be greatly appreciated!任何帮助将不胜感激!

These steps worked out fine for me.这些步骤对我来说效果很好。

  1. Create this folder structure for your layer:为您的图层创建此文件夹结构:

    $ mkdir -p lambda_layers/python/lib/python3.7/site-packages

  2. Get the package into the site-packages folder:将包放入 site-packages 文件夹中:

    $ pip install gspread-pandas -t lambda_layers/python/lib/python3.7/site-packages/.

  3. Create the layer's zip file:创建图层的 zip 文件:

    $ cd lambda_layers

    $ zip -r gspread-pandas_lambda_layer.zip *

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM