无法将文件下载到 AWS Lambda
Cannot download file to AWS Lambda
我有一个下载文件的 AWS Lambda 函数。我读到我唯一可以写入的目录是 /tmp
目录,但我仍然收到此错误。
[ERROR] OSError: [Errno 30] Read-only file system: '/home/sbx_user1051'
Traceback (most recent call last):
File "/var/lang/lib/python3.8/imp.py", line 234, in load_module
return load_source(name, filename, file)
File "/var/lang/lib/python3.8/imp.py", line 171, in load_source
module = _load(spec)
File "<frozen importlib._bootstrap>", line 702, in _load
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 783, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/var/task/lambda_function.py", line 9, in <module>
from kaggle.api.kaggle_api_extended import KaggleApi
File "/var/task/kaggle/__init__.py", line 19, in <module>
from kaggle.api.kaggle_api_extended import KaggleApi
File "/var/task/kaggle/api/__init__.py", line 22, in <module>
from kaggle.api.kaggle_api_extended import KaggleApi
File "/var/task/kaggle/api/kaggle_api_extended.py", line 84, in <module>
class KaggleApi(KaggleApi):
File "/var/task/kaggle/api/kaggle_api_extended.py", line 102, in KaggleApi
os.makedirs(config_dir)
File "/var/lang/lib/python3.8/os.py", line 213, in makedirs
makedirs(head, exist_ok=exist_ok)
File "/var/lang/lib/python3.8/os.py", line 223, in makedirs
mkdir(name, mode)
这是产生错误的代码:
from kaggle.api.kaggle_api_extended import KaggleApi
def lambda_handler(event, context):
api = KaggleApi()
api.authenticate()
api.dataset_download_file(
"gpreda/covid-world-vaccination-progress",
"country_vaccinations.csv",
"/tmp",
)
return {"statusCode": 400, "body": bucket}
正如@joran 所指出的,导入正在尝试创建一些配置目录
config_dir = os.environ.get('KAGGLE_CONFIG_DIR') or os.path.join(
expanduser('~'), '.kaggle')
if not os.path.exists(config_dir):
os.makedirs(config_dir)
您可以设置环境变量 Using AWS Lambda environment variables 或者在这种情况下您可以直接将其指向 /tmp/
因为这是唯一可用于编写任何内容的目录。
对应code
根据文档,您似乎只需要配置凭据,您可以将其放入 AWS Parameter Store 并在您的 lambda 中获取它们。
export KAGGLE_USERNAME=datadinosaur
export KAGGLE_KEY=xxxxxxxxxxxxxx
获取并导出凭据后,您可以为 API 添加导入语句。
或者如果你够冒险可以稍微修改代码并尝试创建一个 Configuration
class 对象最终在初始化中使用它
self.username = ""
# Password for HTTP basic authentication
self.password = ""
我有一个下载文件的 AWS Lambda 函数。我读到我唯一可以写入的目录是 /tmp
目录,但我仍然收到此错误。
[ERROR] OSError: [Errno 30] Read-only file system: '/home/sbx_user1051'
Traceback (most recent call last):
File "/var/lang/lib/python3.8/imp.py", line 234, in load_module
return load_source(name, filename, file)
File "/var/lang/lib/python3.8/imp.py", line 171, in load_source
module = _load(spec)
File "<frozen importlib._bootstrap>", line 702, in _load
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 783, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/var/task/lambda_function.py", line 9, in <module>
from kaggle.api.kaggle_api_extended import KaggleApi
File "/var/task/kaggle/__init__.py", line 19, in <module>
from kaggle.api.kaggle_api_extended import KaggleApi
File "/var/task/kaggle/api/__init__.py", line 22, in <module>
from kaggle.api.kaggle_api_extended import KaggleApi
File "/var/task/kaggle/api/kaggle_api_extended.py", line 84, in <module>
class KaggleApi(KaggleApi):
File "/var/task/kaggle/api/kaggle_api_extended.py", line 102, in KaggleApi
os.makedirs(config_dir)
File "/var/lang/lib/python3.8/os.py", line 213, in makedirs
makedirs(head, exist_ok=exist_ok)
File "/var/lang/lib/python3.8/os.py", line 223, in makedirs
mkdir(name, mode)
这是产生错误的代码:
from kaggle.api.kaggle_api_extended import KaggleApi
def lambda_handler(event, context):
api = KaggleApi()
api.authenticate()
api.dataset_download_file(
"gpreda/covid-world-vaccination-progress",
"country_vaccinations.csv",
"/tmp",
)
return {"statusCode": 400, "body": bucket}
正如@joran 所指出的,导入正在尝试创建一些配置目录
config_dir = os.environ.get('KAGGLE_CONFIG_DIR') or os.path.join(
expanduser('~'), '.kaggle')
if not os.path.exists(config_dir):
os.makedirs(config_dir)
您可以设置环境变量 Using AWS Lambda environment variables 或者在这种情况下您可以直接将其指向 /tmp/
因为这是唯一可用于编写任何内容的目录。
对应code
根据文档,您似乎只需要配置凭据,您可以将其放入 AWS Parameter Store 并在您的 lambda 中获取它们。
export KAGGLE_USERNAME=datadinosaur
export KAGGLE_KEY=xxxxxxxxxxxxxx
获取并导出凭据后,您可以为 API 添加导入语句。
或者如果你够冒险可以稍微修改代码并尝试创建一个 Configuration
class 对象最终在初始化中使用它
self.username = ""
# Password for HTTP basic authentication
self.password = ""