Uploading local files to AWS S3 with boto3 is quite straight forward.

You can install the AWS python SDK boto3 via

1
pip install boto3

Before any implementation, please make sure you have enough permission to interactive with S3.

In order to upload file to S3, you can do something like the the following

1
2
3
4
import boto3

s3res = boto3.resource("s3", region="us-east-1")
s3.meta.client.upload_file("<LOCAL_FILE_PATH>", "<YOUR_BUCKET>", "<YOUR_KEY>")

For example, you can use the above snippet like

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
import boto3

def upload_file(file_name, bucket, key, extra_args=None):
  try:
    s3res = boto3.resource("s3", region="us-east-1")
    s3.meta.client.upload_file(file_name, bucket, key, extra_args)
  except Exception as e:
    print("ERROR: %s" %(e))


upload_file(file_name="/home/eric/project/s3/hello.txt", 
            bucket="eric-us-east-1", 
            key="project/s3/hello.txt", 
            extra_args=extra_args)

If you need to encrypt your data on S3, you can add your configurations in the extra_args argument, and that will be

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
import boto3

def upload_file(file_name, bucket, key, extra_args=None):
  try:
    s3res = boto3.resource("s3", region="us-east-1")
    s3.meta.client.upload_file(file_name, bucket, key, extra_args)
  except Exception as e:
    print("ERROR: %s" %(e))

extra_args = {
  "ServerSideEncryption": "aws:kms",
  "SSEKMSKeyId": "<YOUR_KMS_KEY_ID>"
}
upload_file(file_name="/home/eric/project/s3/hello.txt", 
            bucket="eric-us-east-1", 
            key="project/s3/hello.txt", 
            extra_args=extra_args)