Python只读文件系统错误使用S3和Lambda打开文件进行读取时 [英] Python Read-only file system Error With S3 and Lambda when opening a file for reading

查看:1372
本文介绍了Python只读文件系统错误使用S3和Lambda打开文件进行读取时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm seeing the below error from my lambda function when I drop a file.csv into an S3 bucket. The file is not large and I even added a 60 second sleep prior to opening the file for reading, but for some reason the file has the extra ".6CEdFe7C" appended to it. Why is that?

[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?

If the key/file is "file.csv", then why does the s3.download_file method try to download "file.csv.6CEdFe7C"? I'm guessing when the function is triggered, the file is file.csv.xxxxx but by the time it gets to line 75, the file is renamed to file.csv?

推荐答案

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

Only /tmp seems to be writable in AWS Lambda.

因此这可行:

filepath = '/tmp/' + key

这篇关于Python只读文件系统错误使用S3和Lambda打开文件进行读取时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆