I'm using Box Python API to write some tools. Therefore, one of them is to upload a file to Box. They use a StringIO as the object file.
I need to read a file locally and write its content to the StringIO buffer, then pass that to the Box API as shown in the code below:
def upload_file(self, filename, folder_id='0'):
assert self.client is not None
try:
stream = StringIO.StringIO()
# replace this line a file read
stream.write('Box Python SDK Test!')
stream.seek(0)
box_file = self.client.folder(folder_id=folder_id).upload_stream(
stream, filename,
preflight_check=True)
return box_file.name
except BoxAPIException, e:
self.log.exception(e)
Simple enough, how can I read from a local file, and then write to the StringIO buffer?
stream.write(open(filename).read()).