How to upload binary image data to Google Drive with PyDrive without saving locally

I’m trying to upload image data directly to Google Drive using PyDrive but I’m stuck. I have binary image data from an HTTP request and need to upload it without creating temporary files on disk due to storage limitations.

image_data = requests.get('https://example.com/photo.jpg').content
gdrive_file = drive.CreateFile({'title': 'my_image.jpg'})
# This fails with encoding errors
gdrive_file.SetContentString(image_data.decode('utf-8'))
gdrive_file.Upload()

When I try to decode the binary data as UTF-8, I get UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte. This makes sense since image files contain binary data that isn’t valid UTF-8.

I know I could save the image to a temporary file first and then use SetContentFile(), but I can’t write to the local filesystem. Is there a way to handle this using BytesIO, PIL, or some other method that keeps everything in memory? I need to either convert the binary data properly for SetContentString() or find another approach that works with PyDrive.

try using base64 encoding instead of utf-8. do import base64 then gdrive_file.SetContentString(base64.b64encode(image_data).decode('ascii')) but you’ll need to handle the decoding on googles end. alternatively check if pydrive has a SetContentBytes method or similar - some versions support it directly without needing temp files.

I ran into this exact issue last year and found that PyDrive’s SetContentString() method actually accepts a media_body parameter that can handle binary data directly. Instead of trying to decode the binary image data, you can pass it like this:

from googleapiclient.http import MediaInMemoryUpload

image_data = requests.get('https://example.com/photo.jpg').content
media = MediaInMemoryUpload(image_data, mimetype='image/jpeg')
gdrive_file = drive.CreateFile({'title': 'my_image.jpg'})
gdrive_file.content = media
gdrive_file.Upload()

This approach keeps everything in memory and properly handles the binary data without any encoding issues. Make sure you have the correct mimetype for your image format. Works reliably for images up to several MB in my experience.

Another approach that worked for me is using BytesIO as a file-like object with PyDrive’s file interface. You can wrap your binary data in BytesIO and then use it with the upload methods that expect file objects:

from io import BytesIO

image_data = requests.get('https://example.com/photo.jpg').content
file_buffer = BytesIO(image_data)
gdrive_file = drive.CreateFile({'title': 'my_image.jpg'})
gdrive_file.content = file_buffer
gdrive_file.Upload()

This method treats the binary data as a file stream without touching the filesystem. I’ve used this successfully with various image formats and it handles the binary encoding properly. The key advantage is that BytesIO mimics a file object so PyDrive processes it correctly without trying to interpret the binary data as text.