2015-10-14 33 views
4

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

这是我到目前为止有:

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 
+0

更多详细信息和您当前的代码将有所帮助。你想重新上传文件到S3 gziped,或者只是在本地gzip上做些什么?为什么它必须是lambda函数?你的意思是Python lambda,还是AWS lambda? – Lee

+0

我的意思是使用Python的AWS Lambda函数,因为它现在支持。我在S3上有一个.zip格式的文件,我需要将其更改为.gzip格式。 – Scotty

+0

太好了,谢谢你的澄清。当前的代码会发生什么?它提出了一个例外,或不做你想要的......? – Lee

回答

6

OK,想通弄明白了。感谢您的意见。

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 
+0

问题是什么?你的代码有很多变化。但是将对象保存到一个临时文件中修复它? – Lee

+0

@Lee,我很确定问题是Boto3对象没有被正确读为二进制。因此Scotty必须从S3存储桶下载文件,而不是使用'response ['Body'] .read()'获取 - 这应该是二进制文件的内容。我有同样的问题 - 试图打印'response ['Body']。read()'只是给了我字符串“PK”。不知道这是什么意思。不得不从桶里拉出来很烦人。 – unclemeat

相关问题