Python script to upload files to s3 and boto3

  • Post author:
  • Post category:Python

hello this is a script that will help you upload files to the s3 cloud using python this is a simple script but it can be of great help If you need to upload large files from your computer to the cloud


import boto3
from botocore.exceptions import NoCredentialsError
# enter your secrets key here
ACCESS_KEY = ''
SECRET_KEY = ''


def upload_to_aws(local_file, bucket, s3_file):
    s3 = boto3.client('s3', aws_access_key_id=ACCESS_KEY,
                      aws_secret_access_key=SECRET_KEY)

    try:
        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

#you need to write the path
uploaded = upload_to_aws('<Enter path to local file here>', '<enter bucketname here>', '<store it in this folder using this filename in the s3 bucket>')

I hope it will be very useful if you have any questions do not hesitate to comment and I will gladly help you