Python Script to Upload AWS S3 File with Full Source Code For Beginners

Simple Python script for AWS S3 file upload.


  • boto3 (pip install boto3)

Run the Script:

  • Specify both ACCESS_KEY and SECRET_KEY. You can get them both on your AWS account in “My Security Credentials” section.
  • Specify the local file name, bucket name and the name that you want the file to have inside s3 bucket using LOCAL_FILE, BUCKET_NAME and S3_FILE_NAME variables.
  • Run “python”

Source Code:

import boto3
from botocore.exceptions import NoCredentialsError

LOCAL_FILE = 'local_file_name'
BUCKET_NAME = 'bucket_name'
S3_FILE_NAME = 'file_name_on_s3'

def upload_to_s3(local_file, bucket, s3_file):
    ## This function is responsible for uploading the file into the S3 bucket using the specified credentials. 
    s3 = boto3.client('s3', aws_access_key_id=ACCESS_KEY,
        s3.upload_file(local_file, bucket, s3_file)
        print("Upload Successful")
        return True
    except FileNotFoundError:
        print("The file was not found")
        return False
    except NoCredentialsError:
        print("Credentials not available")
        return False

result = upload_to_s3(LOCAL_FILE, BUCKET_NAME, S3_FILE_NAME)
Code language: PHP (php)

Leave a Comment