[Python] Summary of S3 file operations with boto3

Directory structure

It is assumed that the files are prepared as follows in s3.

line/
└── diagonal/
   └── hoge.csv

Copy files between folders in the same bucket

Create a new straight folder under the line folder and copy line / diagonal / hoge.csv to it.

import os
import boto3

BUCKET_NAME = 'your_bucket'  #Bucket name
COPY_FROM = 'line/diagonal'  #Copy source directory path
COPY_TO = 'line/straight'  #Copy destination directory path]
FILE_NAME = 'hoge.csv'  #file name

s3 = boto3.client('s3')

copy_from_path = os.path.join(COPY_FROM, FILE_NAME)
copy_to_path = os.path.join(COPY_TO, FILE_NAME)

s3.copy_object(Bucket=BUCKET_NAME, key=copy_to_path, CopySource={'Bucket': BUCKET_NAME, 'Key': COPY_FROM_PATH})

When executed, a straight folder will be created under the line folder and hoge.csv will be copied there.

line/
├── diagonal/
│   └── hoge.csv
└── straight/
	└── hoge.csv

Delete files from directory

Delete hoge.csv from the diagonal folder.

import os
import boto3

BUCKET_NAME = 'your_bucket'  #Bucket name
DELETE_DIR_PATH = 'line/diagonal'  #Copy source directory path
FILE_NAME = 'hoge.csv'  #file name

s3 = boto3.client('s3')

delete_file_path = os.path.join(DELETE_DIR_PATH, FILE_NAME)

s3.delete_object(Bucket=BUCKET_NAME, Key=delete_file_path)

When executed, csv will be deleted from hoge.csv under the diagonal folder.

line/
├── diagonal/
│   
└── straight/
	└── hoge.csv

Download locally from S3

import os
import boto3

BUCKET_NAME = 'your_bucket'  #Bucket name
S3_PATH = 'line/straight'  
LOCAL_PATH = 'hogehoge'
FILE_NAME = 'hoge.csv'

s3_resource = boto3.resource('s3')
bucket = s3_resource.Bucket(BUCKET_NAME)

downlod_from_path = os.path.join(S3_PATH, FILE_NAME)
download_to_path = os.path.join(LOCAL_PATH, FILE_NAME)

bucket.download_file(downlod_from_path, download_to_path)

Save Pandas dataframe as csv in S3

import os
import pandas as pd
from io import StringIO
import boto3

S3_PATH = 'line/diagonal'
FILE_NAME = 'diagonal.csv'

df = pd.DataFrame([[1, 10], [2, 20], [3, 30]])

upload_path = os.path.join(S3_PATH, FILE_NAME)

csv_buffer = StringIO()
df.to_csv(csv_buffer)
s3_resource = boto3.resource('s3')
s3_resource.Object(S3_BUCKET, upload_path.put(Body=csv_buffer.getvalue())

When executed, diagonl.csv will be created under the diagonal folder.

line/
├── diagonal/
│   └── diagonal.csv
└── straight/
	└── hoge.csv

Recommended Posts

[Python] Summary of S3 file operations with boto3
Summary of python file operations
Summary of Python3 list operations
Check the existence of the file with python
S3 server-side encryption SSE with Python boto3
Summary of operations often performed with asyncpg
[Python3] Understand the basics of file operations
[Python / DynamoDB / boto3] List of operations I tried
File operations in Python
File operations with open — "../"
File operations in Python
Summary of string operations
S3 uploader with boto
Summary of Python arguments
Convert the character code of the file with Python3
Summary of Excel operations using OpenPyXL in Python
Summary of tools for operating Windows GUI with Python
Issue S3 time-limited URL with boto3 (with file existence confirmation)
Summary of the basic flow of machine learning with Python
Folder creation / file move / compress / delete operations with python
[Memo] Load csv of s3 into pandas with boto3
[S3] CRUD with S3 using Python [Python]
Draw netCDF file with python
Download csv file with python
Extract template of EML file saved from Thunderbird with python3.7
[For beginners] Summary of standard input in Python (with explanation)
Basic summary of data manipulation with Python Pandas-First half: Data creation & manipulation
Extract the xz file with python
Easy encryption of file contents (Python)
[Python] Write to csv file with Python
[Automation with python! ] Part 1: Setting file
Implemented file download with Python + Bottle
A brief summary of Python collections
Output to csv file with Python
Create an Excel file with Python3
Getting Started with Python Basics of Python
Automation of remote operations with Fabric
10 functions of "language with battery" python
Python memo ① Folder and file operations
Implementation of Dijkstra's algorithm with python
ORC, Parquet file operations in Python
[Automation with python! ] Part 2: File operation
Coexistence of Python2 and 3 with CircleCI (1.0)
Use boto3 to mess with S3
Summary of Python indexes and slices
Generate S3 signed URL with boto
Summary of restrictions by file system
Basic study of OpenCV with Python
[OpenCV; Python] Summary of findcontours function
The idea of feeding the config file with a python file instead of yaml
Process the gzip file UNLOADed with Redshift with Python of Lambda, gzip it again and upload it to S3
Basics of binarized image processing with Python
[Examples of improving Python] Learning Python with Codecademy
[Python] Summary of how to use pandas
Python Summary
Creating a simple PowerPoint file with Python
Exclusive control with lock file in Python
Basic summary of scraping with Requests that beginners can absolutely understand [Python]
Read CSV file with python (Download & parse CSV file)
Execute Python script with cron of TS-220
Download the file from S3 using boto.