A python client application that downloads and deletes files from S3 by specifying a bucket

Overview

It was a hassle from the console when I wanted to get all the files from a particular bucket, so I made a tool that can get files at once by specifying a bucket. (Maybe it's already there ...) I tried to add not only acquisition but also deletion processing. (If something happens using this, please do so at your own risk.)

Library used

Tool description

The source itself is available at here. When you start the tool, it will be as follows

image.png

        if aws_access_key_id =='':
            s3 = boto3.resource('s3')
            s3client=boto3.client('s3')
        else:
            s3 = boto3.resource('s3',
                                                aws_access_key_id=aws_access_key_id,
                                                aws_secret_access_key=aws_secret_access_key_id)
            s3client=boto3.client('s3', 
                                                aws_access_key_id=aws_access_key_id,
                                                aws_secret_access_key=aws_secret_access_key_id)

        bucketName=self.inputFileName.get()
        resultFolder=self.outputFolder.get()
        dataBaseDir=os.path.join(resultFolder,bucketName)

        executeType=EXECUTE_LIST.index(self.combo.get())


        s3bucket=s3.Bucket(bucketName)
        objs = s3bucket.meta.client.list_objects_v2(Bucket=s3bucket.name)

        for o in objs.get('Contents'):
            key = o.get('Key')
            s3Paths=os.path.splitext(key)
            if len(s3Paths[1]) !=0:
                keys=key.split('/')
                filename=keys[len(keys)-1]
                if executeType==0:
                    outputDataDir=key.split(filename)[0]
                    outputDataDir=os.path.join(dataBaseDir,outputDataDir)
                    os.makedirs(outputDataDir,exist_ok=True)
                    outputDataFile=os.path.join(outputDataDir,filename)
                    s3bucket.download_file(key,outputDataFile)
                else:
                    s3client.delete_object(Bucket=s3bucket.name, Key=key)
objs = s3bucket.meta.client.list_objects_v2(Bucket=s3bucket.name)

Since it can be obtained in the part of, it is processed by the for statement. It is also possible to specify only a specific folder or file by adding Prefix to the argument.

Recommended Posts

A python client application that downloads and deletes files from S3 by specifying a bucket
A python script that deletes ._DS_Store and ._ * files created on Mac
Create an application that inputs, displays, and deletes forms by using an array as a DB with Python / Flask.
A library that monitors the life and death of other machines by pinging from Python
A note that deployed a Python application from Circle CI to Elastic Beanstalk and notified Slack
Manipulate BigQuery tables from a Python client
Study from Python Reading and writing Hour9 files
A nice nimporter that connects nim and python
"Python Kit" that calls a Python script from Swift
Read line by line from a file with Python
How to execute a schedule by specifying the Python time zone and execution frequency
[Python] Rename all image files in a specific folder by shooting date and time
[Python] A notebook that translates and downloads the ipynb file on GitHub into Japanese.
From a book that programmers can learn ... (Python): Pointer
Copy S3 files from Python to GCS using GSUtil
Get files from Linux using paramiko and scp [Python]
[Python] Delete by specifying a tag with Beautiful Soup
[Python] Start a batch file from Python and pass variables.
A learning roadmap that allows you to develop and publish services from scratch with Python