【发布时间】:2020-08-12 02:06:45
【问题描述】:
我需要解压缩大型 zip 文件(大约 10 GB)并将其放回 S3。我的内存限制为 512 MB。
我尝试了这段代码并在line: 9 获得了MemoryError,它将整个文件内容加载到内存中,因此出现了内存错误。如何检索压缩文件的一部分,将其解压缩并上传回 S3?
import json
import boto3
import io
import zipfile
def lambda_handler(event, context):
s3_resource = boto3.resource('s3')
zip_obj = s3_resource.Object(bucket_name="bucket.name", key="test/big.zip")
buffer = io.BytesIO(zip_obj.get()["Body"].read())
z = zipfile.ZipFile(buffer)
for filename in z.namelist():
s3_resource.meta.client.upload_fileobj(
z.open(filename),
Bucket="bucket.name",
Key=f'{"test/" + filename}'
)
请告诉我
【问题讨论】:
标签: python python-3.x amazon-s3 aws-lambda io