Home > Software engineering >  Upload a file from form in S3 bucket using boto3 and handler is created in lambda
Upload a file from form in S3 bucket using boto3 and handler is created in lambda

Time:12-09

I want to upload image , audio files of small size from form to the S3 using postman for test. I successfully uploaded file in AWS S3 bucket from my application running on my local machine. Following is the part of the code I used for file uploading .

import boto3

s3_client = boto3.client('s3',aws_access_key_id =AWS_ACCESS_KEY_ID,aws_secret_access_key = AWS_SECRET_ACCESS_KEY,)

async def save_file_static_folder(file, endpoint, user_id):       



 _, ext = os.path.splitext(file.filename)

    raw_file_name = f'{uuid.uuid4().hex}{ext}'
    # Save image file in folder
    if ext.lower() in image_file_extension:


        relative_file_folder =user_id '/' endpoint
        
    contents = await file.read()

    try:
        response = s3_client.put_object(Bucket = S3_BUCKET_NAME,Key = (relative_file_folder '/' raw_file_name),Body = contents)
        
    except:
        return FileEnum.ERROR_ON_INSERT

I called this function from another endpoint and form data (e.g. name, date of birth and other details) are successfully saved in Mongodb database and files are uploaded in S3 bucket.

This app is using fastapi and files are uploaded in S3 bucket while deploying this app in local machine.

Same app is delpoyed in AWS lambda and S3 bucket as storage. For handling whole app , following is added in endpoint file.

handler = Mangum(app)

After deploying app in AWS creating lambda function from root user account of AWS, files didnot get uploaded in S3 bucket.

If I didnot provide files during form then the AWS API endpoint successfully works. Form data gets stored in MongoDB database (Mongodb atlas) and app works fine hosted using Lambda.

App deployed using Lambda function works successfully except file uploads in form. FOr local machine, file uploads in S3 get success.

EDIT

While tracing in Cloudwatch I got following error

exception  An error occurred (InvalidAccessKeyId) when calling the PutObject operation: The AWS Access Key Id you provided does not exist in our records.

I checked AWS Access Key Id and secret key many times and they are correct and root user credentials are kept.

CodePudding user response:

It looks like you have configured your Lambda function with an execution IAM role, but you are overriding the AWS credentials supplied to the boto3 SDK here:

s3_client = boto3.client('s3',aws_access_key_id =AWS_ACCESS_KEY_ID,aws_secret_access_key = AWS_SECRET_ACCESS_KEY,)

You don't need to provide credentials explicitly because the boto3 SDK (and all language SDKs) will automatically retrieve credentials dynamically for you. So, ensure that your Lambda function is configured with the correct IAM role, and then change your code as follows:

s3_client = boto3.client('s3')

As an aside, you indicated that you may be using AWS root credentials. It's generally a best security practice in AWS to not use root credentials. Instead, create IAM roles and IAM users.

We strongly recommend that you do not use the root user for your everyday tasks, even the administrative ones. Instead, adhere to the best practice of using the root user only to create your first IAM user. Then securely lock away the root user credentials and use them to perform only a few account and service management tasks.

  • Related