Sunday, 22 September 2019

AWS S3 upload files | Upload file to S3 using python | Lambda upload files to S3 bucket

S3 is storage provided by AWS. It stores data inside buckets.
We can upload data to s3 using boto3 library. Here is code which also works for AWS lambda functions.

Below is sample code to upload files to S3 using python :

import json
import boto3 
import requests

access_key='your_access_key'
secret_access='your_secret_access'
region = 'your_region'
s3 = boto3.resource('s3', region_name=region, aws_access_key_id=access_key,
                                   aws_secret_access_key=secret_access)
  
def s3_uplaod(bucket, domain,content):
    file_path = '{}/{}'.format(domain,"name_of_file")
    object = s3.Object(bucket, file_path)
    object.put(Body=content)

def main(job):
    try:
        bucket = 'your_bucket_name'
        domain = 'sub-folder_inside_bucket'
        content = 'local_location_of_file'
        s3_uplaod(bucket, domain,content)
    except Exception as e:
        print(e)

def handler(event, context):
    main(event)
handler('a','a')

Notes:

  1. You can replace required parameters like keys and bucket name.
  2. domain : used where we have folder inside bucket.
  3. if path doesn't exist inside bucket it will create required path.
  4. You can skip handler part if not using code for AWS lambda functions.
You can comment below if you face any issues here.


5 comments:

  1. It was extremely helpful to understand the basic conflict management options. Applying these to a specific issue that I am dealing with helped me get past a big hurdle. Thank you!DevOps Training in Chennai

    DevOps Online Training in Chennai

    DevOps Training in Bangalore

    DevOps Training in Hyderabad

    DevOps Training in Coimbatore

    DevOps Training

    DevOps Online Training

    ReplyDelete
  2. This article was exactly what I needed to read today.
    also, check Python training in Nagpur

    ReplyDelete
  3. Thanks for sharing article with us .
    Python Training in Solapur

    ReplyDelete
  4. great post , learn things from your blog thanks for this information. Are you intresting in code developer then checkout python training in satara

    ReplyDelete