I have a Python function [1] which requires the file path pointing to the file it has to process as an argument. However, I have a string variable containing the contents of the respective file. I don't want to create a file only to read it with said function and then delete the file.
Is there a way to pass a variable instead of a file path pointing to a file with the same contents?
Example of the problem:
data = '1,2,3\n4,5,6'
# this is the intended function use:
process_data('data.csv')
# this is how I would like to use it
# so that I don't have to create a file
process_data(data)
[1] Specifically, said function is boto3.client.upload_file(file_path, bucket, target_path)
I've read through some ways to use io.StringIO
but somehow I'm unlucky to get them running.
boto3.client.upload_file
expects a filename and not a file object, hence why you cannot use io.StringIO
with it. However you can use boto3.client.upload_fileobj
, which precisely works with file objects, eg.
import io
import boto3
s3 = boto3.client('s3')
f = io.StringIO('1,2,3\n4,5,6')
s3.upload_fileobj(f, ...)