Home > Software engineering >  Not able to see the file I'm downloading from S3 bucket in /tmp/ in lambda
Not able to see the file I'm downloading from S3 bucket in /tmp/ in lambda

Time:11-09

I am not able to see the downloaded file from s3 bucket in /tmp/ folder of lambda.I have provided full access over S3 and Lambda in IAM policy. When I'm testing this code, my tmp folder is always empty(refer screenshot). enter image description here

Can some-one help me out??

My code :

import csv
import boto3


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

def lambda_handler(event, context):
    bucket = 'test-bucket'
    key = 'data/original_file1.csv'
    try:
        temp_path= '/tmp/test_file.csv'
        s3_resource.Bucket(bucket).download_file(key,temp_path)
        
        response = s3_client.get_object(Bucket=bucket, Key=key)
        return response['ContentType']
        
        
    except Exception as e:
        print(e)

CodePudding user response:

There is a misunderstanding here. You seem to assume that the directory structure you see in the Editor in the Management Console is the same that's being used in the Lambda function. This is not the case.

The online editor you're using essentially let's you edit the content of the .zip file that contains the function code for your Lambda.

Whenever the Lambda function is executed and a new execution context ("instance") of the function is required, the Lambda service will start a new micro-VM, download the code assets and extract them to the micro-VM. You then get to access the /tmp/ directory in that execution context.

This is completely disconnected from the editor, so you can't see the changes being made in the /tmp/ directory within the execution context.

  • Related