AWS Lambda,将S3文件保存到/ tmp目录

Vie*_*ike 4 amazon-s3 amazon-web-services python-3.x aws-lambda

我想从S3复制一组文件,然后在运行lambda函数时将它们放在/ tmp目录中,以使用和操作内容。以下代码摘录在我的PC(运行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
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试在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)"
    ]
  ]
}
Run Code Online (Sandbox Code Playgroud)

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

Joh*_*ein 8

您应该保存/tmp,而不是保存tmp/

例如:

local_file_name = '/tmp/' + KEY
Run Code Online (Sandbox Code Playgroud)

  • @MichaelBrant我做了一个测试,并通过在 Lambda 函数中使用 `os.mkdir('/tmp/foo')` 在 `/tmp/` 中成功创建了一个目录。 (4认同)
  • 请注意,您不能在 /tmp/ 中创建目录。文件必须直接进入/tmp/ (3认同)
  • @lorless 前导的“/”表示绝对路径。仅使用“tmp/”将是相对于“当前”目录的相对偏移量,而不是“/”。 (2认同)