首页 > 解决方案 > AWS Lambda,将 S3 文件保存到 /tmp 目录

问题描述

我想从 S3 复制一组文件,并在我的 lambda 函数运行时将它们放在 /tmp 目录中,以使用和操作内容。以下代码摘录在我的电脑(正在运行 Windows)上运行良好

s3 = boto3.resource('s3')
BUCKET_NAME = 'car_sentiment'
keys = ['automated.csv', 'connected_automated.csv', 'connected.csv', 
        'summary.csv']
for KEY in keys: 
    try:
        local_file_name = 'tmp/'+KEY
        s3.Bucket(BUCKET_NAME).download_file(KEY, local_file_name)
    except botocore.exceptions.ClientError as e:
        if e.response['Error']['Code'] == "404":
            continue
        else:
            raise

但是,当我尝试在 AWS lambda 上运行时,我得到:

{
  "errorMessage": "[Errno 2] No such file or directory: 'tmp/automated.csv.4Bcd0bB9'",
  "errorType": "FileNotFoundError",
  "stackTrace": [
    [
      "/var/task/SentimentForAWS.py",
      28,
      "my_handler",
      "s3.Bucket(BUCKET_NAME).download_file(KEY, local_file_name)"
    ],
    [
      "/var/runtime/boto3/s3/inject.py",
      246,
      "bucket_download_file",
      "ExtraArgs=ExtraArgs, Callback=Callback, Config=Config)"
    ],
    [
      "/var/runtime/boto3/s3/inject.py",
      172,
      "download_file",
      "extra_args=ExtraArgs, callback=Callback)"
    ],
    [
      "/var/runtime/boto3/s3/transfer.py",
      307,
      "download_file",
      "future.result()"
    ],
    [
      "/var/runtime/s3transfer/futures.py",
      73,
      "result",
      "return self._coordinator.result()"
    ],
    [
      "/var/runtime/s3transfer/futures.py",
      233,
      "result",
      "raise self._exception"
    ],
    [
      "/var/runtime/s3transfer/tasks.py",
      126,
      "__call__",
      "return self._execute_main(kwargs)"
    ],
    [
      "/var/runtime/s3transfer/tasks.py",
      150,
      "_execute_main",
      "return_value = self._main(**kwargs)"
    ],
    [
      "/var/runtime/s3transfer/download.py",
      582,
      "_main",
      "fileobj.seek(offset)"
    ],
    [
      "/var/runtime/s3transfer/utils.py",
      335,
      "seek",
      "self._open_if_needed()"
    ],
    [
      "/var/runtime/s3transfer/utils.py",
      318,
      "_open_if_needed",
      "self._fileobj = self._open_function(self._filename, self._mode)"
    ],
    [
      "/var/runtime/s3transfer/utils.py",
      244,
      "open",
      "return open(filename, mode)"
    ]
  ]
}

为什么它认为文件名tmp/automated.csv.4Bcd0bB9不仅仅是文件名tmp/automated.csv,我该如何解决?一直在努力解决这个问题,尝试了多种方法,其中一些方法在我的 PC 上本地运行时会产生类似的错误。谢谢!

标签: python-3.xamazon-web-servicesamazon-s3aws-lambda

解决方案


你应该保存在/tmp,而不是tmp/

例如:

local_file_name = '/tmp/' + KEY

推荐阅读