8

AWS ラムダ関数で 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
4

1 に答える 1