因此,我正在创建一项将文件从S3存储桶复制到驱动器文件夹的服务。在S3存储桶/文件夹上创建对象时,它将触发Lambda。我抓取该文件(大型TXT文件),“本地”复制其内容,以便能够通过其API上传并驱动。程序运行良好,但大文件除外。我的内存不足:
[Errno 28] No space left on device: OSError
Traceback (most recent call last):
File "/var/task/lambda_function.py", line 67, in lambda_handler
raise e
File "/var/task/lambda_function.py", line 56, in lambda_handler
f.write(chunk[0:last_newline+1].decode('utf-8'))
OSError: [Errno 28] No space left on device
这是生成该问题的代码,文件约为270 Mb。在配置了2Gb内存的情况下测试正在运行的Lambda。
obj = s3.get_object(Bucket = bucket,Key=key) #Getting the object that triggered the Lambda
fullpath="/tmp/"+key #To create a "local file" keeping its original name
os.chdir('/tmp')
f= open(fullpath,"w+") #Open the file to start writing in it.
body = obj['Body']
chunk_size = 1000000 * 10 #Reading it in chunks of ~10MB
newline = '\n'.encode()
partial_chunk = b''
while (True):
chunk = partial_chunk + body.read(chunk_size)
if chunk == b'':
break
last_newline = chunk.rfind(newline)
f.write(chunk[0:last_newline+1].decode('utf-8')) #Writing to the file (GETTING OUT OF MEMORY HERE)
f.flush()
# keep the partial line you've read here
partial_chunk = chunk[last_newline+1:]
f.close()
upload(parent,filename,key,fullpath)
对于上传,我正在做:
upload():
...
drive_service = build('drive', 'v3', credentials=credenciales)
media_body1 = MediaFileUpload(fullpath, resumable=True,chunksize=1024 * 1024 * 5) #Using 5MB chunks
body1={'name':fullpath,'parents':[parent]}#,'media_body':content}
Filesubido = drive_service.files().create(body=body1, media_body=media_body1, supportsAllDrives='True')
response = None
while response is None:
time.sleep(5)
status, response = Filesubido.next_chunk()
if status:
print ("Uploaded "+ str(int(status.progress() * 100)))
print ("Upload Complete!")
我尝试过的事情:
content=obj['Body'].read().decode('utf-8')
Lambda内存不足,因此我以大块的形式读取它,然后将其保存到文件中,但是现在我用完了“存储”(500MB最大,但文件为270MB)。问题:
/tmp
上吗?干杯!
AWS Lambda函数可以使用/tmp
驱动器上的512MB空间。当Lambda函数完成使用临时文件后,应删除该文件,以便该空间可用于将来重复使用同一Lambda容器的函数执行。
只要您的文件小于512MB,就可以正常使用。
如果它们大于512MB,那么您将需要从Amazon S3读取数据块并将其上传到目的地,从而遍历所有块。这使得阅读和写作都变得更加复杂。
或者,您可以使用其他计算服务,例如Amazon EC2或Fargate。