It is assumed that the files are prepared as follows in s3.
line/
└── diagonal/
└── hoge.csv
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 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
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)
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