Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here
Sign InSign Up

Stackorigin – The Community of Question and Answers

Stackorigin – The Community of Question and Answers Logo Stackorigin – The Community of Question and Answers Logo
Search
Ask A Question

Mobile menu

Close
Ask a Question
Home/Boto3 Python Crash Course with AWS S3

Boto3 Python Crash Course with AWS S3

Boto3 Python Crash Course with AWS S3
  • What is Boto3?
  • Installation
  • To create a new IAM user follow bellow steps
  • Creating new bucket
  • Upload file’s into s3 bucket
  • Get all files from s3 bucket
  • Get all prefix file’s from s3 bucket
  • Deleting files from s3 bucket

What is Boto3?

Boto3 is the Amazon Web Services (AWS) Software Development Kit (SDK) for Python, which allows Python developers to write software that makes use of services like Amazon S3.

Installation:

Once you set up your environment then install Boto3 on your computer by using below command

pip install boto3

To create a new IAM user follow bellow steps

  • Log into your AWS Management Console and select the IAM service.
  • In the side navigation menu, select Access management | Users, and then select Add user.
  • In the User name field, enter the name of the new user. In the Access type field, check the Programmatic access option to allow the user only programmatic access.
  • Select Attach existing policies directly, and then select policy Name.
  • Select Next: Tags  and then Select Next: Review.
  • Download and save the credentials of the new user (Access key iD and Secret access key).

AWS Client Vs Resource

Client and Resource are two different abstractions within the boto3 SDK for making AWS service requests.

  • Client is original boto3 API abstraction.
  • Client provide low-level AWS service access.
  • all AWS service operations are supported by clients.
  • Resource is the newer boto3 API abstraction.
  • Resource provide high-level, object-oriented API.
  • does not provide 100% API coverage of AWS services.

Creating new bucket:

You can create new Bucket name Programmatically also. Remember this bucket name must be unique throughout the whole AWS platform. If you try to create a bucket name, but that bucket name has already created then you will receive the following error: botocore.errorfactory.BucketAlreadyExists.

import boto3
def check_bucket_name():
    s3 = boto3.resource('s3',aws_access_key_id='',aws_secret_access_key='')
    data = s3.create_bucket(Bucket="botopy")
    if data:
        print("successfully bucket is created")
check_bucket_name()

Upload file’s into s3 bucket: You can upload different file format(txt, image, csv, xls, xlsx and json) into s3 bucket.

import boto3
def upload():
    s3 = boto3.resource('s3', aws_access_key_id="", aws_secret_access_key="")
    data = s3.Object('botopy', 'data/test.txt').put(Body=open('E:\projects\portfolio/aws\s3/test.txt', 'rb'))
    if data:
        print("files is stored into botopy bucket")
upload()

Get all files from s3 bucket:

You can get all files from specific folder. Here bototype is bucket name and prefix is subfolder’s.

import boto3
def get_files():
    s3 = boto3.resource('s3', aws_access_key_id="", aws_secret_access_key="")
    my_bucket = s3.Bucket('botopy')
    r = [obj.key for obj in my_bucket.objects.filter(Delimiter='/', Prefix='data/')]
    print("list of all files", r)
get_files()

Get all prefix file’s from s3 bucket:

You can get list of all files starting with given string/prefix.

import boto3, os
def get_files():
    s3 = boto3.resource('s3', aws_access_key_id="", aws_secret_access_key="")
    my_bucket = s3.Bucket('botopy')
    r = [obj.key for obj in my_bucket.objects.filter(Delimiter='/', Prefix='data/')]
    final_data = []
    for x in range(len(r)):
        d = os.path.basename(r[x])
        if d.startswith("test"):
            final_data.append(d)
    return final_data
print(get_files())

Deleting files from s3 bucket:

You can delete the files from particular s3 path.

import boto3
def delete_files():
    s3 = boto3.resource('s3', aws_access_key_id="", aws_secret_access_key="")
    data = s3.Object("botopy", 'data/test444.txt')
    data.delete()
delete_files()

Share
  • Facebook

Sidebar

Ask A Question

Stats

  • Questions 1k
  • Answers 1k
  • Best Answers 80
  • Users 81

Adv 250x250

Adv 234x60

  • Recent
  • Answers
  • ashok

    How do I get the current time?

    • 2 Answers
  • ashok

    How to Install and Use Git on Windows

    • 1 Answer
  • ashok

    How to Check Git Version

    • 2 Answers
  • arjun

    Which institute is best for a data science course in ...

    • 1 Answer
  • ashok

    git stash and git pull

    • 1 Answer
  • pratap
    pratap added an answer How do I get the current time in Python: The time module… June 29, 2022 at 9:08 am
  • pratap
    pratap added an answer How do I get the current time in Python: Use datetime:… June 29, 2022 at 9:04 am
  • hari
    hari added an answer How to Install and Use Git on Windows Git is… June 29, 2022 at 8:24 am
  • sam
    sam added an answer How to Check Git Version: $ git --version git version… June 29, 2022 at 8:13 am
  • sam
    sam added an answer How to Check Git Version: In a command prompt: $… June 29, 2022 at 8:13 am

New Members

Sakshigupta1998

Sakshigupta1998

  • 0 Questions
  • 0 Answers
legal heir certificate online apply

legal heir certificate online apply

  • 0 Questions
  • 0 Answers
Mutual divorce process india

Mutual divorce process india

  • 0 Questions
  • 0 Answers
propertyregistration

propertyregistration

  • 0 Questions
  • 0 Answers
Best Gynecologist Hospital in Delhi

Best Gynecologist Hospital in Delhi

  • 0 Questions
  • 0 Answers

Adv 234x60

Trending Categories

Programming
825Questions
, 0Followers
Technology
209Questions
, 3Followers
General Knowledge
127Questions
, 0Followers
Business & Finance
81Questions
, 4Followers
Employment
73Questions
, 3Followers

Trending Tags

django (95) git (57) google (29) india (35) mysql (22) oil (54) pandas (23) python (241) usa (25) youtube (24)

Recent posts

    • On: June 29, 2022

    Bitbucket Cloud recently stopped supporting account passwords for Git authentication

    • On: June 21, 2022

    Best Telegram Movie Channels

    • On: June 17, 2022

    How to Open ICICI Bank Savings Account

    • On: June 15, 2022

    How to Download Your Own Twitch Videos

    • On: June 14, 2022

    How To Permanently Delete Your PSN Account

Explore Our Blog

Adv 234x60

Subscribe

Explore

  • Recent Questions
  • Most Answered
  • Answers
  • No Answers
  • Most Visited
  • Most Voted
  • Random

Footer

Stackorigin - The Community of Question and Answers

Stackorigin

Stackorigin is the world’s largest Q&A networking site, Stackorigin community brings you the collaboration of all the various Questions and the related Answers given by the community.

About

  • About Us
  • Contact Us
  • FAQ
  • Submit Guest Post Article on Technology, Education, Health, Apps, Gadgets, IoT, AI, Business, Digital Marketing and More

Info

  • Privacy Policy
  • Terms and Conditions
  • Community Guidelines
  • Tags

Products

  • Tutorials
  • Advertising
  • Categories
  • Corona
  • StackHow

Follow

© 2022 Stackorigin. All Rights Reserved.