python读写文件错误_Python只读文件系统错误使用S3和Lambda打开文件进行读取时

1586010002-jmsa.png

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'

Code:

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)

The result of the above is:

mytestbucket

file.csv

/file.csv

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

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?

解决方案

Only /tmp seems to be writable in AWS Lambda.

Therefore this would work:

filepath = '/tmp/' + key

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值