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

标签 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

最佳答案

好的,明白了。感谢您的意见,李。

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

关于python - 使用 AWS lambda 函数使用 boto3 python 将 S3 文件从 zip 转换为 gzip,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33130115/

相关文章:

python - Pytorch 中 NLLLoss 损失函数的 C 类是什么?

python - 如何将 Python 列表中的后续数字连接成双(或更多)数字

java - TCP Java 服务器,Python 客户端字符串不相等

javascript - 在 JavaScript 中检索亚马逊产品信息

mysql - 无法插入 Amazon RDS 数据库

amazon-web-services - 具有 SAM 的 API 网关未正确更新

python - 如何更改机器人状态但仅在一台服务器中可见

javascript - javascript 中的 gunzip

C# Gzip 在服务器端错误地压缩字节数组

c - 解压大于可用内存的gz文件