I'm trying to download a .png image via HTTP requests and upload it via HTTP to another location. My objective is to avoid saving the file on the disk so it's processed in-memory.
I have the code below:
- Download the file and convert it into a byte array:
resp = requests.get(
'http://www.personal.psu.edu/crd5112/photos/PNG%20Example.png',
stream=True)
img = BytesIO(resp.content)
- Upload the file to a remote HTTP repository
data=open(img.getvalue()).read()
r = requests.post(url=url, data=data, headers=headers, auth=HTTPBasicAuth('user', 'user'))
I'm getting a ValueError exception "embedded null byte" when reading the byte array.
If I save the file onto the disk and load it as below, then there is no error:
with open('file.png', 'wb') as pic:
pic.write(img.getvalue())
Any advice on how I could achieve it without saving the file on the disk ?