Upload image as bytes.
@sqrrrl đang làm issue này rồi.
Từ ngày 3/3/2023.
Đánh giá
Issue này chưa được đánh giá.
Mô tả
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)
- Ngôn ngữ chính
- Python
- Star
- 1.4k
- Fork
- 917
- Merge trung bình
- 3 phút
- Pull request đã merge (30 ngày)
- 1
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của googleworkspace/python-samples
-
ايقاف البحث الأمنCó thể làm lại được @sqrrrl đã nhận 400 ngày trước và không có pull request nào đang mở. Đang mở
googleworkspace/python-samples#2434 · 1 người được giao ·
-
Slow Uploads using Google Drive API with PythonCó thể làm lại được @sqrrrl đã nhận 740 ngày trước và không có pull request nào đang mở. Đang mở
googleworkspace/python-samples#1852 · 1 bình luận · 1 người được giao ·
-
403 error from google after a few successful runs of my codeCó thể làm lại được @vinay-google đã nhận 912 ngày trước và không có pull request nào đang mở. Đang mở
googleworkspace/python-samples#1424 · 1 người được giao ·
-
How to see the progress of files which is getting uploadedCó thể làm lại được @vinay-google đã nhận 928 ngày trước và không có pull request nào đang mở. Đang mở
googleworkspace/python-samples#1404 · 1 người được giao ·
-
Phyton SamplesCó thể làm lại được @PierrickVoulet đã nhận 975 ngày trước và không có pull request nào đang mở. Đang mở
googleworkspace/python-samples#1279 · 1 người được giao ·
Tất cả issue của googleworkspace/python-samples
Issue tương tự
-
[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 retryingCó thể đã có người làm @yipinghuang1991 đã nhận hôm nay. Đang mởcomp/agent P2 type/bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
NousResearch/hermes-agent#156837 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Bug: Missing continue on ValueError in CpanBackend.check_feed causes UnboundLocalError or duplicate yieldsCó thể đã có người làm @muskan-creates352 đã nhận hôm nay. Đang mở
Độ khó 1/5 1-3 giờ Mức phù hợp với người mới 86/100
fedora-infra/anitya#2188 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
docs documentation need-triage SaaS
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
Maintainer thường phản hồi trong vòng 1 ngày
-
integration:hermes
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 85/100
vectorize-io/hindsight#6601 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
triage:product type:bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
dbt-labs/dbt-adapters#2333 ·
Maintainer thường phản hồi trong vòng 1 ngày