访问无服务器 Lambda 函数中的 /tmp 或其他临时文件结构

access to /tmp or other temporary file structure in Serverless Lambda Function

使用控制台的 AWS Lambda 函数

在 AWS Lambda 中,我的函数会将文件从 S3 保存到 /tmp 目录,如下所示:

local_filepath = '/tmp/file.txt'

s3.download_file(
  Bucket=bucket,
  Key=key,
  Filename=local_filepath
)

生活也很美好。

使用无服务器的 AWS Lambda 函数

然而,使用无服务器是另一回事。

相同的设置导致以下错误:

[Errno 2] No such file or directory: '/tmp/processed.txt.7E4850BD'

所以我猜 Serverless 执行环境中没有 /tmp 目录。

我尝试使用 local_filepath = 'file.txt' 将文件保存到当前目录,但出现 OSError(30, 'Read-only file system') 错误。

尝试使用 tempfile 模块,它有一组技术来检索要使用的临时目录:

local_filepath = os.path.join(tempfile.gettempdir(), 'file.txt')