본문 바로가기
파이썬

파이썬 Save Dataframe to csv directly to s3 Python

by º기록 2020. 11. 2.
반응형

새 CSV 파일에 업로드하려는 pandas DataFrame이 있습니다. 문제는 파일을 s3로 전송하기 전에 로컬에 저장하고 싶지 않다는 것입니다. 데이터 프레임을 s3에 직접 쓰는 to_csv와 같은 방법이 있습니까? boto3를 사용하고 있습니다.
지금까지 내가 가지고있는 것은 다음과 같습니다.

import boto3
s3 = boto3.client('s3', aws_access_key_id='key', aws_secret_access_key='secret_key')
read_file = s3.get_object(Bucket, Key)
df = pd.read_csv(read_file['Body'])

# Make alterations to DataFrame

# Then export DataFrame to CSV through direct transfer to s3

 

해결 방법

 

당신이 사용할 수있는:

from io import StringIO # python3; python2: BytesIO 
import boto3

bucket = 'my_bucket_name' # already created on S3
csv_buffer = StringIO()
df.to_csv(csv_buffer)
s3_resource = boto3.resource('s3')
s3_resource.Object(bucket, 'df.csv').put(Body=csv_buffer.getvalue())

 

참조 페이지 https://stackoverflow.com/questions/38154040

 

 

반응형

댓글