Upload image as bytes.

未關閉
#778 0 則留言 0 個 reaction 已指派 1 人 在 GitHub 檢視

@sqrrrl 已經在處理了。

開始於 2023年3月3日。

評估

這個 Issue 還沒有評估資料。

描述

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)
主要語言
Python
星號
1.4k
分支
917
平均合併
3 分鐘
30 天內合併 PR
1

環境準備

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

googleworkspace/python-samples 的其他 Issue

查看 googleworkspace/python-samples 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。