[PYTHON] I wanted to delete multiple objects in s3 with boto3

install boto3

$ pip3 install boto3

Settings for accessing s3 are file

aws.py


import boto3

s3 = boto3.client(
    's3',
    aws_access_key_id = {access key},
    aws_secret_access_key = {secret_access_key},
    region_name = 'ap-northeast-1')
    
s3_bucket = 'sample-app'

Create a method to operate s3

services/s3_service.py


from aws import s3, s3_bucket

class S3Service:

    #Get the contents of an object
    def get_object():
        s3_dir = 'image/sample.json'
        sample_json = s3.get_object(Bucket=s3_bucket, Key=s3_dir)
        body = sample_json['Body'].read()

        print(body.decode('utf-8'))

    #Get multiple objects
    def get_objects(id):
        s3_dir = 'image/{0}/'.format(id)
        image_objs = s3.list_objects_v2(Bucket=s3_bucket, Prefix=s3_dir)

        for image in image_objs['Contents']: 
            print(image['Key'])

    #Delete multiple objects
    def delete_objects(id):
        s3_dir = 'image/{0}/'.format(id)
        image_objs = s3.list_objects_v2(Bucket=s3_bucket, Prefix=s3_dir)

        for obj in image_objs['Contents']: 
            key = obj['Key']
            s3.delete_object(Bucket=s3_bucket, Key=key)

call service class

controller/image_controller.py


from flask import Blueprint, make_response
from services.s3_service import S3Service

# set route
sample_controller = Blueprint('sample_controller_router', __name__)

@sample_controller.route('/delete/<id>', methods=['PUT'])
def delete_images(id):
   S3Service.delete_objects(id)
   return make_response(jsonify({'code': 200}))

Recommended Posts

I wanted to delete multiple objects in s3 with boto3
Use boto3 to mess with S3
I want to detect objects with OpenCV
I wanted to solve ABC160 with Python
I really wanted to copy with selenium
Implemented DQN in TensorFlow (I wanted to ...)
I wanted to solve NOMURA Contest 2020 with Python
I tried to integrate with Keras in TFv1.1
I want to display multiple images with matplotlib.
I wanted to install Python 3.4.3 with Homebrew + pyenv
Easily log in to AWS with multiple accounts
I wanted to know the number of lines in multiple files, so I tried to get it with a command
I also wanted to check type hints with numpy
How to get multiple model objects randomly in Django
I want to transition with a button in flask
Manipulate S3 objects with Boto3 (high-level API and low-level API)
Delete 1000 objects stored in AWS S3 at a time.
I was addicted to scraping with Selenium (+ Python) in 2020
How to embed multiple embeds in one message with Discord.py
I wanted to solve the Panasonic Programming Contest 2020 with Python
I want to easily delete columns containing NA in R
I tried two ways to combine multiple commits in Git
S3 operation with python boto3
I can't log in to the admin page with Django3
I captured the Touhou Project with Deep Learning ... I wanted to.
I wanted to calculate an array with Sympy's subs method
How to Delete with SQLAlchemy?
I tried to create an article in Wiki.js with SQLAlchemy
I tried to describe the traffic in real time with WebSocket
I want to solve APG4b with Python (only 4.01 and 4.04 in Chapter 4)
I wanted to create a smart presentation with Jupyter Notebook + nbpresent
What I was addicted to with json.dumps in Python base64 encoding
I tried to process the image in "sketch style" with OpenCV
I wanted to do something like an Elixir pipe in Python
I wanted to solve the ABC164 A ~ D problem with Python
I tried to process the image in "pencil style" with OpenCV
Two ways to display multiple graphs in one image with matplotlib
How to delete multiple specified positions (indexes) in a Python list
I tried to log in to twitter automatically with selenium (RPA, scraping)
Connect to multiple databases with SQLAlchemy
Hash chain I wanted to avoid (2)
I wanted to evolve cGAN to ACGAN
6 ways to string objects in Python
Connect to Elastic MQ with boto
Delete multiple elements in python list
I want to debug with Python
Hash chain I wanted to avoid (1)
Generate S3 signed URL with boto
I tried to delete bad tweets regularly with AWS Lambda + Twitter API
I made a tool to automatically browse multiple sites with Selenium (Python)
(Matplotlib) I want to draw a graph with a size specified in pixels
How to deal with SSL error when connecting to S3 with boto of Python
It's more recent, but I wanted to do BMI calculation with python.
[Introduction to Python] How to delete rows that meet multiple conditions in Pandas.DataFrame
If you want to get multiple statistics with groupby in pandas v1
Environment maintenance made with Docker (I want to post-process GrADS in Python
[Google Colab] I want to display multiple images side by side in tiles
Upload what you got in request to S3 with AWS Lambda Python
Copy data from Amazon S3 to Google Cloud Storage with Python (boto)
I tried to implement PLSA in Python
I tried multiple regression analysis with polynomial regression