首页 > 解决方案 > Python Lambda 用于复制大文件

问题描述

我在 python2.7 中组合了一个简单的 Lambda 函数,当文件放入存储桶时会触发。后来我发现预计要发送到我观看的存储桶的文件可能大于 5GB(找到的记录约为 490GB)。所以用不同的复制函数更新函数。但是,我现在所做的编辑会导致事件触发器出现问题。

使用其他复制功能时可能导致错误的原因是什么?有什么方法可以改善 <500GB 文件的情况吗?

具有 5GB 副本限制的原始脚本:

from __future__ import print_function

import json
import boto3
import time
import urllib
import re

print("Loading sftp file mover. Fuction= dev-sftp-move-lambda")
s3 = boto3.client('s3')             #for s3 copy function
s3_resource = boto3.resource('s3')  #for s3 delete function

.... checks for fileparts and other stuff...

def lambda_handler(event, context):
    source_bucket = event['Records'][0]['s3']['bucket']['name']
    key = urllib.unquote_plus(event['Records'][0]['s3']['object']['key'])
    target_bucket = 'dev-sftpinput'
    copy_source = {'Bucket':source_bucket,'Key':key}
    try:
        s3.copy_object(Bucket=target_bucket, Key=key, CopySource=copy_source, ServerSideEncryption='AES256')
    except Exception as e:
    print(e)
    print('Error in execution')
    raise

为了允许更大的文件,我尝试简单地将复制行替换为:

s3_resource.meta.client.copy( Bucket=target_bucket, Key=key, CopySource=copy_source, ExtraArgs={'ServerSideEncryption':'AES256'})

产生的错误:

'Records': KeyError
Traceback (most recent call last):
  File "/var/task/lambda_function.py", line 19, in lambda_handler
    source_bucket = event['Records'][0]['s3']['bucket']['name']
KeyError: 'Records'

标签: python-2.7lambdaboto3

解决方案


推荐阅读