从 S3 下载文件时,AWS Lambda 中出现“只读文件系统”错误

问题描述 投票:0回答:5

当我将 file.csv 放入 S3 存储桶时,我在 lambda 函数中看到以下错误。该文件并不大,我什至在打开文件进行读取之前添加了 60 秒的睡眠时间,但由于某种原因,该文件附加了额外的“.6CEdFe7C”。这是为什么?

[Errno 30] Read-only file system: u'/file.csv.6CEdFe7C': IOError
Traceback (most recent call last):
File "/var/task/lambda_function.py", line 75, in lambda_handler
s3.download_file(bucket, key, filepath)
File "/var/runtime/boto3/s3/inject.py", line 104, in download_file
extra_args=ExtraArgs, callback=Callback)
File "/var/runtime/boto3/s3/transfer.py", line 670, in download_file
extra_args, callback)
File "/var/runtime/boto3/s3/transfer.py", line 685, in _download_file
self._get_object(bucket, key, filename, extra_args, callback)
File "/var/runtime/boto3/s3/transfer.py", line 709, in _get_object
extra_args, callback)
File "/var/runtime/boto3/s3/transfer.py", line 723, in _do_get_object
with self._osutil.open(filename, 'wb') as f:
File "/var/runtime/boto3/s3/transfer.py", line 332, in open
return open(filename, mode)
IOError: [Errno 30] Read-only file system: u'/file.csv.6CEdFe7C'

代码:

def lambda_handler(event, context):

    s3_response = {}
    counter = 0
    event_records = event.get("Records", [])

    s3_items = []
    for event_record in event_records:
        if "s3" in event_record:
            bucket = event_record["s3"]["bucket"]["name"]
            key = event_record["s3"]["object"]["key"]
            filepath = '/' + key
            print(bucket)
            print(key)
            print(filepath)
            s3.download_file(bucket, key, filepath)

上面的结果是:

mytestbucket
file.csv
/file.csv
[Errno 30] Read-only file system: u'/file.csv.6CEdFe7C'

如果密钥/文件是“file.csv”,那么为什么 s3.download_file 方法会尝试下载“file.csv.6CEdFe7C”?我猜测当函数被触发时,文件是 file.csv.xxxxx 但当它到达第 75 行时,文件被重命名为 file.csv?

python amazon-s3 aws-lambda boto3
5个回答
370
投票

似乎只有

/tmp
在 AWS Lambda 中可写。

因此这会起作用:

filepath = '/tmp/' + key

参考资料:


22
投票

根据http://boto3.readthedocs.io/en/latest/guide/s3-example-download-file.html

示例展示了如何使用第一个参数为云名称,第二个参数为要下载的本地路径。

另一方面,amazaon 文档

因此,我们有 512 MB 用于创建文件。 这是我在 lambda aws 上的代码,它的工作原理就像魅力一样。

.download_file(Key=nombre_archivo,Filename='/tmp/{}'.format(nuevo_nombre))

1
投票

我注意到,当我上传 lambda

directly as a zip file
的代码时,我只能写入
/tmp
文件夹,但是当从
S3
上传代码时,我也能够写入
project root folder


0
投票

对于 C# 来说也很完美:

using (var fileStream = File.Create("/tmp/" + fName))
{
   str.Seek(0, SeekOrigin.Begin);
   str.CopyTo(fileStream);
}

0
投票

"""lambda 函数只允许在 /tmp 路径中写入。

首先简单地这样做。它会像魅力一样发挥作用。 ”“”

导入操作系统

os.chdir(“/tmp/”)

© www.soinside.com 2019 - 2024. All rights reserved.