使用API​​网关从S3下载大文件?

时间:2019-02-25 18:42:25

标签: python amazon-web-services amazon-s3 aws-lambda aws-api-gateway

我正在创建一个网页,用户可以单击不同的链接来下载文件,大小从1兆字节到300兆字节不等。这些链接向API网关端点执行GET请求,该网关从S3下载相应的文件。我目前正在使用以下代码:

def files_download(event, context):
try:
    key = event['queryStringParameters']['key']
    # name = event
except Exception as e:
    return client_failure('Key field required as query parameter', e)

try:
    file = s3.Object('XXXXX', key)

    return success_file('text/csv', key, file.get()['Body'].read())
except Exception as e:
    return server_failure('Failed to retrieve file', e)

def success_file(content_type, attachment_name, data):
    return {
        'isBase64Encoded': False,
        'statusCode': 200,
        'headers': {
            'Access-Control-Allow-Methods': '*',
            'Access-Control-Allow-Origin': '*',
            'Access-Control-Allow-Credentials' : True,
            'Content-Disposition': 'attachment; filename=' + attachment_name
        },
        'body': data
    }

def success(data):
    return {
        'isBase64Encoded': False,
        'statusCode': 200,
        'headers': {
            'Content-Type': 'application/json',
            'Access-Control-Allow-Methods': '*',
            'Access-Control-Allow-Origin': '*',
            'Access-Control-Allow-Credentials' : True
        },
        'body': json.dumps({
            'data': data
        }, cls=DecimalEncoder)
    }

def server_failure(message, details):
    return {
        'isBase64Encoded': False,
        'statusCode': 500,
        'headers': {
            'Content-Type': 'application/json',
            'Access-Control-Allow-Methods': '*',
            'Access-Control-Allow-Origin': '*',
            'Access-Control-Allow-Credentials' : True
        },
        'body': json.dumps({
            'message': message,
            'details': str(details)
        }, cls=DecimalEncoder)
    }

def client_failure(message, details):
    return {
        'isBase64Encoded': False,
        'statusCode': 400,
        'headers': {
            'Content-Type': 'application/json',
            'Access-Control-Allow-Methods': '*',
            'Access-Control-Allow-Origin': '*',
            'Access-Control-Allow-Credentials' : True
        },
        'body': json.dumps({
            'message': message,
            'details': str(details)
        }, cls=DecimalEncoder)
    }

但是,这对于大型文件(> 100 MB)似乎失败。有什么方法可以使其正常工作,还是有更好的方法允许用户下载文件?我曾考虑过公开我的S3存储桶并提供直接链接,但是显然出于身份验证的原因,这并不理想。

0 个答案:

没有答案