Lambda 可以使用 open() 方法从 S3 打开的最大文件大小

Maximum file size from S3 a Lambda can open using open() method

我目前正在开发一些 lambda 来对 S3 上托管的文本文件执行 Python 脚本。

这些文本文件可能会很大(最多 1GB),据我所知,Lambda 有一个 512Mb 的 tmp 目录,所以我假设我只能加载一个 512MB 的文件。

但我也看到它有高达 10240MB 的函数内存分配。

那么我可以使用 open() 方法从 S3 打开一个 1GB 的文件吗?

如果有人也可以给我一些关于 tmp 文件夹和内存的区别的新手的见解 ==> 如果内存是 10GB 为什么要使用 512MB tmp 文件夹?

非常感谢!

祝 2022 年愉快

您可以使用正则 get_object,而无需将其写入 /tmp:

s3 = boto3.client('s3')

def lambda_handler(event, context):

    response = s3.get_object(
        Bucket='your-bucket',
        Key='your-key'
    )
    
    # get the content of the file as bytes 
    text_bytes = response['Body'].read()
    
    # change it to string
    text_str  = text_bytes.decode()

    # process as you want the text_str