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 a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

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

Adv 120x600

Looking for advertising?

Consider These Things If You Appreciate What stackorigin Does: Stackorigin is a community where you can ask questions and find answers, for free to everyone. We would appreciate it if you bought us a coffee if you liked what we were doing.

Buy me a Coffe

Adv 120x600

  • Random
  • Answers
    • On: April 2, 2021
    • Answers: 0

    How do I download and install postman in Ubuntu?

    • On: September 13, 2021
    • Answer: 1

    How To Set Up A Python Virtual Environment On Windows

    • On: August 3, 2022
    • Answers: 0

    Tricortone Online NO PRESCRIPTION Reviews, Tricortone for hives

    • On: July 27, 2020
    • Answer: 1

    What are the best online shopping sites in India?

    • On: February 4, 2022
    • Answers: 0

    Where do snipping tool snips get saved?

  • strapcart_online
    strapcart_online added an answer Sildenafil citrate is a common ingredient in Cenforce 150mg tablets. This drug… March 21, 2023 at 11:42 am
  • strapcart_online
    strapcart_online added an answer Vilitra 20mg tablets are used to overcome the problem of… March 18, 2023 at 6:13 am
  • ashok
    ashok added an answer Not every girl, there are some girls out there who… March 16, 2023 at 4:15 am
  • strapcart_online
    strapcart_online added an answer Tadarise 40 Tablet is used to permanently remove the problem… March 4, 2023 at 5:48 am
  • strapcart_online
    strapcart_online added an answer Men use Cenforce D tablets for the treatment of sexual… March 1, 2023 at 5:59 am

Adv 120x600

Trending Categories

Health
2041Questions
, 4Followers
Programming
1236Questions
, 0Followers
Technology
216Questions
, 3Followers
General Knowledge
131Questions
, 0Followers
Business & Finance
84Questions
, 4Followers

Adv 120×600

Stats

  • Questions 4k
  • Answers 1k
  • Best Answers 105
  • Users 100

Users

niyadas823

niyadas823

  • 0 Questions
  • 0 Answers
nirala3

nirala3

  • 2 Questions
  • 0 Answers
Perrywalton

Perrywalton

  • 1 Question
  • 0 Answers
veeraa

veeraa

  • 0 Questions
  • 0 Answers
Stewesmitz

Stewesmitz

  • 0 Questions
  • 0 Answers

Adv 120×600

Explore

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

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.