Save Dataframe to csv directly to s3 Python

前端 未结 10 922
独厮守ぢ
独厮守ぢ 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:48

    I found this can be done using client also and not just resource.

    from io import StringIO
    import boto3
    s3 = boto3.client("s3",\
                      region_name=region_name,\
                      aws_access_key_id=aws_access_key_id,\
                      aws_secret_access_key=aws_secret_access_key)
    csv_buf = StringIO()
    df.to_csv(csv_buf, header=True, index=False)
    csv_buf.seek(0)
    s3.put_object(Bucket=bucket, Body=csv_buf.getvalue(), Key='path/test.csv')
    

提交回复
热议问题