使用AWS lambda函数将S3文件从zip转换为gzip,使用boto3 python

时间:2015-10-14 15:57:18

标签: python amazon-web-services gzip aws-lambda boto3

我需要在AWS lambda函数中使用boto3 python将.zip文件从S3转换为.gzip文件。有关如何做到这一点的任何建议吗?

这是我到目前为止所做的:

import json
import boto3
import zipfile
import gzip

s3 = boto3.resource('s3')

def lambda_handler(event, context):

    bucket = event['Records'][0]['s3']['bucket']['name']
    key = event['Records'][0]['s3']['object']['key']

    try: 
        s3Obj = s3.Object(bucket_name=bucket, key=key)
        response = s3Obj.get()
        data = response['Body'].read()
        zipToGzip = gzip.open(data, 'wb')
        zipToGzip.write(s3.upload_file(bucket, (s3 + '.gz')))
        zipToGzip.close()
    except Exception as e:
        print(e)
        print('Error getting object {} from bucket {}. Make sure they exist and your bucket is in the same region as this function.'.format(key, bucket))
        raise e

1 个答案:

答案 0 :(得分:8)

好的,弄清楚了。感谢您输入李。

import json
import boto3
import zipfile
import gzip

print('Loading function')

s3 = boto3.resource('s3')
s3_client = boto3.client('s3')

def lambda_handler(event, context):

    # Get the object from the event and show its content type
    bucket = event['Records'][0]['s3']['bucket']['name']
    key = event['Records'][0]['s3']['object']['key']

    try: 
        s3_client.download_file(bucket, key, '/tmp/file.zip')
        zfile = zipfile.ZipFile('/tmp/file.zip')
        namelist = zfile.namelist()

        if len(namelist) >1:
            pass
            #alertme()

        for filename in namelist:
            data = zfile.read(filename)
            f = open('/tmp/' + str(filename), 'wb')
            f.write(data)
            f.close()

        zipToGzip = gzip.open('/tmp/data.gz', 'wb')
        zipToGzip.write(data)
        zipToGzip.close()
        s3_client.upload_file('/tmp/data.gz', bucket, key + '.gz')
        s3_client.delete_object(Bucket=bucket, Key=key)
    except Exception as e:
        print(e)
        print('Error getting object {} from bucket {}. Make sure they exist and your bucket is in the same region as this function.'.format(key, bucket))
        raise e