使用PyDrive将图片上传到Google云端硬盘

奥尔多·苏哈托诺·普特拉

我对PyDrive有一个愚蠢的问题。我尝试使用FastAPI创建REST API,该API将使用PyDrive将图像上传到Google Drive。这是我的代码:

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'

我应该怎么做才能完成这个非常简单的任务?

谢谢你的帮助!

**

更新

**

感谢Stanislas Morbieu的回答和评论,这是我更新的工作代码:

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}

多谢你们

斯坦尼斯拉斯·莫比乌

SetContentString需要一个str不是类型的参数bytes这里是文档:

将此文件的内容设置为字符串。

创建utf-8编码string的io.BytesIO实例如果未指定,则将mimeType设置为'text / plain'。

因此,您应该在utf-8中解码img_file(类型为bytes):

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

pydrive:尝试将文件从远程服务器上传到Google云端硬盘

使用gapi将文件上传到Google云端硬盘

使用php curl将文件上传到Google云端硬盘

使用Dropzone将文件上传到Google云端硬盘

使用意图将文件上传到Google云端硬盘

将文件上传到Google云端硬盘?

在后台将文件上传到Google云端硬盘

Angular Gapi将简单文本上传到Google云端硬盘

将文件从网络上传到Google云端硬盘

将文件从Blobstore上传到Google云端硬盘

使用http上传到Google云端硬盘文件夹

使用Curl将文件上传到Google云端硬盘会显示401

使用Android将文本文件上传到Google云端硬盘

使用cURL将文件上传到Google云端硬盘-access_token过期问题

使用Windows服务自动将文件上传到Google云端硬盘文件流

使用javascript(无html表单)以编程方式将文件上传到Google云端硬盘

Python:如何使用folderId将文件上传到Google云端硬盘中的特定文件夹

如何使用Apps脚本将云端硬盘中的现有文件上传到云端硬盘

如何使用Java App Engine将图片文件正确上传到Google云端存储?

Google云端硬盘API将文件上传到Java团队硬盘

如何使用REST API将新修订版本上传到Google云端硬盘中的特定文件?

无法将文件上传到Google云端硬盘中的文件夹

无法将文件上传到Google云端硬盘上的特定文件夹

将文件上传到Google云端硬盘后,如何处理该事件(调用脚本)?

将内存中的文件下载并上传到Google云端硬盘

将文件上传到Google云端硬盘中的特定文件夹

如何将统一的网络播放器游戏上传到Google云端硬盘?

以编程方式将视频上传到Google云端硬盘(Android API)

通过Javascript API将多个文件上传到Google云端硬盘的最佳策略