Загрузить изображение на Google Drive с помощью PyDrive - PullRequest
0 голосов
/ 27 октября 2019

У меня глупый вопрос о PyDrive. Я пытаюсь создать REST API с помощью FastAPI, который будет загружать изображение на Google Drive с помощью PyDrive. Вот мой код:

from fastapi import FastAPI, File
from starlette.requests import Request
from starlette.responses import JSONResponse
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive

app = FastAPI()


@app.post('/upload')
def upload_drive(img_file: bytes=File(...)):
    g_login = GoogleAuth()
    g_login.LoadCredentialsFile("google-drive-credentials.txt")

    if g_login.credentials is None:
        g_login.LocalWebserverAuth()
    elif g_login.access_token_expired:
        g_login.Refresh()
    else:
        g_login.Authorize()
    g_login.SaveCredentialsFile("google-drive-credentials.txt")
    drive = GoogleDrive(g_login)

    file_drive = drive.CreateFile({'title':'test.jpg'})
    file_drive.SetContentString(img_file) 
    file_drive.Upload()

После попытки доступа к моей конечной точке я получаю эту ошибку:

file_drive.SetContentString(img_file)
  File "c:\users\aldho\anaconda3\envs\fastai\lib\site-packages\pydrive\files.py", line 155, in SetContentString
    self.content = io.BytesIO(content.encode(encoding))
AttributeError: 'bytes' object has no attribute 'encode'

Что я должен сделать, чтобы выполнить эту очень простую задачу?

спасибо за вашу помощь!

**

ОБНОВЛЕНО

**

Спасибо за ответ и комментарий от Станислава Морбиу, вот мой обновленный ирабочий код:

from fastapi import FastAPI, File
from starlette.requests import Request
from starlette.responses import JSONResponse
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from PIL import Image
import os

app = FastAPI()


@app.post('/upload')
def upload_drive(filename, img_file: bytes=File(...)):
    try:
        g_login = GoogleAuth()
        g_login.LocalWebserverAuth()
        drive = GoogleDrive(g_login)

        file_drive = drive.CreateFile({'title':filename, 'mimeType':'image/jpeg'})

        if not os.path.exists('temp/' + filename):
            image = Image.open(io.BytesIO(img_file))
            image.save('temp/' + filename)
            image.close()

        file_drive.SetContentFile('temp/' + filename)
        file_drive.Upload()

        return {"success": True}
    except Exception as e:
        print('ERROR:', str(e))
        return {"success": False}

Спасибо, ребята

1 Ответ

2 голосов
/ 27 октября 2019

SetContentString требует параметр типа str, а не bytes. Вот документация:

Установить содержимое этого файла в виде строки.

Создает экземпляр io.BytesIO из строки в кодировке utf-8 . Устанавливает для mimeType значение «text / plain», если оно не указано.

Поэтому необходимо декодировать img_file (типа bytes) в utf-8:

file_drive.SetContentString(img_file.decode('utf-8'))
...