Save Dataframe to csv directly to s3 Python

前端 未结 10 925
独厮守ぢ
独厮守ぢ 2020-11-28 02:02

I have a pandas DataFrame that I want to upload to a new CSV file. The problem is that I don\'t want to save the file locally before transferring it to s3. Is there any meth

10条回答
  •  一整个雨季
    2020-11-28 02:44

    since you are using boto3.client(), try:

    import boto3
    from io import StringIO #python3 
    s3 = boto3.client('s3', aws_access_key_id='key', aws_secret_access_key='secret_key')
    def copy_to_s3(client, df, bucket, filepath):
        csv_buf = StringIO()
        df.to_csv(csv_buf, header=True, index=False)
        csv_buf.seek(0)
        client.put_object(Bucket=bucket, Body=csv_buf.getvalue(), Key=filepath)
        print(f'Copy {df.shape[0]} rows to S3 Bucket {bucket} at {filepath}, Done!')
    
    copy_to_s3(client=s3, df=df_to_upload, bucket='abc', filepath='def/test.csv')
    

提交回复
热议问题