iterative / PyDrive2

Google Drive API Python wrapper library. Maintained fork of PyDrive.
https://docs.iterative.ai/PyDrive2
Other
580 stars 69 forks source link

Upload binary data directly from memory #224

Closed covalschi closed 2 years ago

covalschi commented 2 years ago

Hello everyone, I have a feature request.

Can we have a method for GoogleDriveFile to SetContent directly from memory? I have a ton of files on remote servers I have to download from there and upload to google drive, but I don't want to save them to disk just to be read to memory right after.

Thanks for keeping the project alive!

shcheklein commented 2 years ago

@covalschi how about SetContentString? Also, you can do something similar to its implementation:

        self.content = io.BytesIO(content.encode(encoding))
        if self.get("mimeType") is None:
            self["mimeType"] = "text/plain"

e.g:

file.content = io.BytesIO( )

It means that you could pass any file-like object, stream to it.

covalschi commented 2 years ago

Of course I've tried that, but the problem is with encode(encoding) part - some files just can't be utf-8 encoded.

shcheklein commented 2 years ago

so, you could pass any bytes stream to it ... file.content = ... (no need to use SetContentString). Up to you which encoding to use. By default it's utf-8 / text. But even SetContentString can be adjusted, you could pass encoding if you want.

covalschi commented 2 years ago

no need to use SetContentString

Ah, never thought about it, thank you!