python - 从 S3 下载文件时,AWS Lambda 中出现错误 "Read-only file system"

标签 python amazon-s3 aws-lambda boto3

当我将 file.csv 放入 S3 存储桶时,我的 lambda 函数出现以下错误。该文件并不大,我什至在打开文件进行读取之前添加了 60 秒的 sleep 时间,但由于某种原因,该文件附加了额外的“.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'

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

最佳答案

在 AWS Lambda 中似乎只有 /tmp 是可写的。

因此这会起作用:

filepath = '/tmp/' + key

引用资料:

关于python - 从 S3 下载文件时,AWS Lambda 中出现错误 "Read-only file system",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39383465/

相关文章:

android - Proguard Aws s3 问题

python - 为什么我们需要 gevent.queue?

python - 迭代有效地计算平均值

python - 如何用Python读取水印?

ios - 错误 : The request signature we calculated does not match the signature you provided. 检查您的 key 和签名方法

python - 将 django-storages 后端从 s3 更改为 cloudfiles 并处理旧文件

node.js - SageMaker NodeJS 的 SDK 未锁定 API 版本

python - 如何以 json 格式将 pandas 数据帧 to_json() 写入 s3

amazon-web-services - 通过 API 网关从 Lambda 返回 HTTP 状态代码

python - 我可以根据将键与两个互斥键列表匹配来从单个 python 字典理解中产生两个总和吗?