Search code examples
pythonamazon-web-servicesioboto3

Upload data to S3 bucket without saving it to a disk


Ther is the boto3 method upload_file(Filename='path') that uses the Filename parameter to read a file from a disk and upload it to a bucket. Is it possible to upload data without saving it to a disk?


Solution

  • Save text file:

    obj = 'some string'
    bucket = 'my-bucket'
    key = 'prefix/filename.txt'
    
    boto3.client('s3').put_object(Body=obj, Bucket=bucket, Key=key)
    

    Save csv file from pandas dataframe:

    df = my-dataframe
    bucket = 'my-bucket'
    key = 'prefix/filename.csv'
    
    csv_buffer = io.StringIO()
    df.to_csv(csv_buffer)
    boto3.client('s3').put_object(Body=csv_buffer.getvalue(), Bucket=bucket, Key=key)