Upload image as bytes.
@sqrrrl y travaille déjà.
Depuis le 3/3/2023.
Évaluation
Cette issue n'a pas encore été évaluée.
Description
Summary
Hi. I am trying to upload a image to google drive that is in bytes format. However, I get the following error:
UnicodeDecodeError Traceback (most recent call last)
g:\My Drive\xFiles\Work\ReMedia\KID\MIO52\Python\workspace.ipynb Cell 19 in <cell line: 6>()
3 credentials = Credentials.from_authorized_user_info(info={"client_id": GOOGLE_CLIENT_ID, "client_secret": GOOGLE_CLIENT_SECRET, "refresh_token": GOOGLE_REFRESH_TOKEN})
4 service = googleapiclient.discovery.build('drive', 'v3', credentials=credentials)
----> 6 media = MediaFileUpload(screenshot,mimetype='image/png',)
7 file_metadata = {'name': f'{adid}.png', 'parents': [drive_folder_id]}
10 try:
File c:\Users\andri\Pythonenvs\googleapi\lib\site-packages\googleapiclient_helpers.py:131, in positional..positional_decorator..positional_wrapper(*args, **kwargs)
129 elif positional_parameters_enforcement == POSITIONAL_WARNING:
130 logger.warning(message)
--> 131 return wrapped(*args, **kwargs)
File c:\Users\andri\Pythonenvs\googleapi\lib\site-packages\googleapiclient\http.py:593, in MediaFileUpload.init(self, filename, mimetype, chunksize, resumable)
591 self._fd = None
592 self._filename = filename
--> 593 self._fd = open(self._filename, "rb")
594 if mimetype is None:
595 # No mimetype provided, make a guess.
596 mimetype, _ = mimetypes.guess_type(filename)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte
Using the following code:
The code:
Upload file to Google Drive
credentials = Credentials.from_authorized_user_info(info={"client_id": GOOGLE_CLIENT_ID, "client_secret": GOOGLE_CLIENT_SECRET, "refresh_token": GOOGLE_REFRESH_TOKEN})
service = googleapiclient.discovery.build('drive', 'v3', credentials=credentials)
media = MediaFileUpload(screenshot,mimetype='image/png',)
file_metadata = {'name': f'{adid}.png', 'parents': [drive_folder_id]}
try:
file = service.files().create(body=file_metadata, media_body=media, fields='id').execute()
print(f'File ID: {file.get("id")} uploaded successfully to folder ID: {drive_folder_id}')
except HttpError as error:
print(f'An error occurred: {error}')
file = None
NOTE: sceenshot is in bytes format
If I change to io.BytesIO(screenshot) I get the error:
TypeError Traceback (most recent call last)
g:\My Drive\xFiles\Work\ReMedia\KID\MIO52\Python\workspace.ipynb Cell 19 in <cell line: 6>()
3 credentials = Credentials.from_authorized_user_info(info={"client_id": GOOGLE_CLIENT_ID, "client_secret": GOOGLE_CLIENT_SECRET, "refresh_token": GOOGLE_REFRESH_TOKEN})
4 service = googleapiclient.discovery.build('drive', 'v3', credentials=credentials)
----> 6 media = MediaFileUpload(BytesIO(screenshot),mimetype='image/png',)
7 file_metadata = {'name': f'{adid}.png', 'parents': [drive_folder_id]}
10 try:
File c:\Users\andri\Pythonenvs\googleapi\lib\site-packages\googleapiclient_helpers.py:131, in positional..positional_decorator..positional_wrapper(*args, **kwargs)
129 elif positional_parameters_enforcement == POSITIONAL_WARNING:
130 logger.warning(message)
--> 131 return wrapped(*args, **kwargs)
File c:\Users\andri\Pythonenvs\googleapi\lib\site-packages\googleapiclient\http.py:593, in MediaFileUpload.init(self, filename, mimetype, chunksize, resumable)
591 self._fd = None
592 self._filename = filename
--> 593 self._fd = open(self._filename, "rb")
594 if mimetype is None:
595 # No mimetype provided, make a guess.
596 mimetype, _ = mimetypes.guess_type(filename)
TypeError: expected str, bytes or os.PathLike object, not BytesIO
Any suggestions>?
Specifications
- Python version (
3.10) - OS (Windows)
- Langage dominant
- Python
- Étoiles
- 1.4k
- Forks
- 917
- Merge moyen
- 3 min
- PR mergées (30 j)
- 1
Préparer son environnement
- Aucun Dockerfile ni fichier Docker Compose
- Propose un modèle de pull request
- Lire le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de googleworkspace/python-samples
-
ايقاف البحث الأمنPeut-être à nouveau libre @sqrrrl l’a pris il y a 400 jours, et aucune pull request n’est ouverte. Ouverte
googleworkspace/python-samples#2434 · 1 personne assignée ·
-
Slow Uploads using Google Drive API with PythonPeut-être à nouveau libre @sqrrrl l’a pris il y a 740 jours, et aucune pull request n’est ouverte. Ouverte
googleworkspace/python-samples#1852 · 1 commentaire · 1 personne assignée ·
-
403 error from google after a few successful runs of my codePeut-être à nouveau libre @vinay-google l’a pris il y a 912 jours, et aucune pull request n’est ouverte. Ouverte
googleworkspace/python-samples#1424 · 1 personne assignée ·
-
How to see the progress of files which is getting uploadedPeut-être à nouveau libre @vinay-google l’a pris il y a 928 jours, et aucune pull request n’est ouverte. Ouverte
googleworkspace/python-samples#1404 · 1 personne assignée ·
-
Phyton SamplesPeut-être à nouveau libre @PierrickVoulet l’a pris il y a 975 jours, et aucune pull request n’est ouverte. Ouverte
googleworkspace/python-samples#1279 · 1 personne assignée ·
Toutes les issues de googleworkspace/python-samples
Issues similaires
-
bug
Difficulté 2/5 1-3 heures Accessibilité débutants 77/100
canonical/paas-charm#407 · 1 commentaire ·
Les mainteneurs répondent en général sous 5 jours
-
bug
Difficulté 1/5 Moins d'une heure Accessibilité débutants 90/100
FAIRmat-NFDI/pynxtools-spm#136 ·
Les mainteneurs répondent en général sous 1 jour
-
enhancement tooling
Difficulté 2/5 1-3 heures Accessibilité débutants 84/100
Les mainteneurs répondent en général sous 1 jour
-
`read_nexus` entry point points to a function that no longer existsPeut-être pris @RubelMozumder l’a pris aujourd’hui. Ouverteannotator bug
Difficulté 1/5 Moins d'une heure Accessibilité débutants 88/100
FAIRmat-NFDI/pynxtools#876 · 1 commentaire ·
Les mainteneurs répondent en général sous 1 jour
-
[Bug]: HTTP 499 (Client Closed Request) is classified as a non-retryable format_error, so a mid-flight abort fails the turn over instead of retryingPeut-être pris @yipinghuang1991 l’a pris aujourd’hui. Ouvertecomp/agent P2 type/bug
Difficulté 2/5 1-3 heures Accessibilité débutants 82/100
NousResearch/hermes-agent#156837 ·
Les mainteneurs répondent en général sous 1 jour