Search code examples
pythonamazon-web-servicesamazon-s3aws-lambdaamazon-sqs

this lambda should open a file uploaded to the s3 bucket, but it tells me the object doesn't exist


This AWS Lambda function should open a file uploaded to the s3 bucket, but it tells me the object doesn't exist:

import os
import boto3
import botocore
import zipfile
import gzip
import logging
import json
import urllib



s3_client = boto3.resource('s3')
my_bucket = s3_client.Bucket('test.s3')

def lambda_handler(event, context):
    try:
        for record in event['Records']:
            body = json.loads(record['body'])
            key = body['Records'][0]['s3']['object']['key']
            bucket_name = body['Records'][0]['s3']['bucket']['name']
        
        encoding_key = urllib.parse.unquote_plus(key, encoding='utf-8')
        print(encoding_key)
        message = 'This file got uploaded '+ "'" + encoding_key + "'" + ' to this bucket ' + "'" + bucket_name + "'"
        print('test')
        print(message)
    
    
        path, filename = os.path.split(key)
        my_bucket.download_file(key, f"/tmp/{filename}")
        # response = s3.get_object(Bucket=bucket_name, key=key)
       
        #zip
        if filename.endswith('.zip'):
            with zipfile.ZipFile(f"/tmp/{filename}", 'r') as zip_obj:
                listZip = zip_obj.namelist()
                
                for file in listZip:
                    with zip_obj.open(file) as myfile:
                        print(myfile.read())
                        zip_obj.close()
        #gz            
        elif filename.endswith('.gz'):
            gzfile = gzip.open(f"/tmp/{filename}", 'r')
            
            file_content = gzfile.read()
            print(file_content)
            gzfile.close()
            
        else:
            with open(f"/tmp/{filename}", 'r') as file:
                print(file.read())
                    
    except botocore.exceptions.ClientError as e:
        if e.response['Error']['Code'] == "404":
            print("The object does not exist.")
        else:
            raise

Solution

  • Something looks wrong with the for loop. I think you'll need:

            for record in event['Records']:
                key = record['s3']['object']['key']
                bucket_name = record['s3']['bucket']['name']
    

    If there are strange characters in the key, you can use:

    key = urllib.parse.unquote_plus(record['s3']['object']['key'], encoding='utf-8')
    

    If you do, you'll also need to import urllib.