Tôi thấy lỗi dưới đây từ hàm lambda của tôi khi tôi thả tệp.csv vào thùng S3. Tệp không lớn và thậm chí tôi đã thêm một giấc ngủ 60 giây trước khi mở tệp để đọc, nhưng vì lý do nào đó tệp có thêm ".6CEdFe7C" được thêm vào. Tại sao vậy?Python Hệ thống tệp chỉ đọc Lỗi Với S3 và Lambda khi mở tệp để đọc
[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)
Kết quả trên là:
mytestbucket
file.csv
/file.csv
[Errno 30] Read-only file system: u'/file.csv.6CEdFe7C'
Nếu phím/file là "file.csv", thì tại sao phương pháp s3.download_file cố gắng tải xuống "file.csv.6CEdFe7C"? Tôi đoán khi chức năng được kích hoạt, tập tin là file.csv.xxxxx nhưng vào thời điểm nó được đưa vào dòng 75, tập tin được đổi tên thành file.csv?
Dump không bằng để đọc! Vì vậy, tập tin của bạn trên thư mục temp (hoặc trong ram) cần dump không 'self._osutil.open (tên tập tin, 'wb') là f:', chỉ cho phép 'rb'etc. Vì vậy, cần xử lý tệp nguồn trước khi xử lý. – dsgdfg