简体   繁体   English

使用 pydrive 将图像字符串上传到 Google Drive

[英]Uploading image string to Google Drive using pydrive

I need to upload an image string (as the one you get from requests.get(url).content ) to google drive using the PyDrive package.我需要使用 PyDrive package 将图像字符串(作为您从requests.get(url).content获得的字符串)上传到谷歌驱动器。 I checked a similar question but the answer accepted there was to save it in a temporary file on a local drive and then upload that.我检查了一个类似的问题,但接受的答案是将其保存在本地驱动器上的临时文件中,然后上传。
However, I cannot do that because of local storage and permission restrictions.但是,由于本地存储和权限限制,我不能这样做。
The accepted answer was previously to use SetContentString(image_string.decode('utf-8')) since之前接受的答案是使用SetContentString(image_string.decode('utf-8'))因为

SetContentString requires a parameter of type str not bytes . SetContentString需要str而不是bytes类型的参数。

However the error: UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte came up, as in the comments on that answer.但是错误: UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte出现,如对该答案的评论。
Is there any way to do this without using a temporary file, using PIL / BytesIO /anything that can convert it to be uploaded correctly as a string or somehow using PIL manipulated as an image and uploaded using SetContentFile() ?有没有办法在不使用临时文件的情况下做到这一点,使用PIL / BytesIO /anything 可以将其转换为正确上传为字符串或以某种方式使用 PIL 作为图像操作并使用SetContentFile()上传?

A basic example of what I'm trying to do is:我正在尝试做的一个基本示例是:

img_content = requests.get('https://i.imgur.com/A5gIh7W.jpeg')
file = drive.CreateFile({...})
file.setContentString(img_content.decode('utf-8'))
file.Upload()

When I saw the document ( Upload and update file content ) of pydrive, it says as follows.当我看到 pydrive 的文档( 上传和更新文件内容)时,它说如下。

Managing file content is as easy as managing file metadata.管理文件内容就像管理文件元数据一样简单。 You can set file content with either SetContentFile(filename) or SetContentString(content) and call Upload() just as you did to upload or update file metadata.您可以使用 SetContentFile(filename) 或 SetContentString(content) 设置文件内容,然后调用 Upload(),就像上传或更新文件元数据一样。

And, I searched about the method for directly uploading the binary data to Google Drive.并且,我搜索了将二进制数据直接上传到 Google Drive 的方法。 But, I couldn't find it.但是,我找不到它。 From this situation, I thought that there might not be such method.从这种情况来看,我认为可能没有这种方法。 So, in this answer, I would like to propose to upload the binary data using requests module.所以,在这个答案中,我想建议使用requests模块上传二进制数据。 In this case, the access token is retrieved from the authorization script of pydrive.在这种情况下,访问令牌是从 pydrive 的授权脚本中检索的。 The sample script is as follows.示例脚本如下。

Sample script:示例脚本:

from pydrive.auth import GoogleAuth
import io
import json
import requests


url = 'https://i.imgur.com/A5gIh7W.jpeg' # Please set the direct link of the image file.
filename = 'sample file' # Please set the filename on Google Drive.
folder_id = 'root' # Please set the folder ID. The file is put to this folder.

gauth = GoogleAuth()
gauth.LocalWebserverAuth()
metadata = {
    "name": filename,
    "parents": [folder_id]
}
files = {
    'data': ('metadata', json.dumps(metadata), 'application/json'),
    'file': io.BytesIO(requests.get(url).content)
}
r = requests.post(
    "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart",
    headers={"Authorization": "Bearer " + gauth.credentials.access_token},
    files=files
)
print(r.text)

Note:笔记:

  • In this script, it supposes that your URL is the direct link of the image file.在此脚本中,假设您的 URL 是图像文件的直接链接。 Please be careful this.请注意这一点。

  • In this case, uploadType=multipart is used.在这种情况下,使用uploadType=multipart The official document says as follows.官方文档是这样说的。 Ref参考

    Use this upload type to quickly transfer a small file (5 MB or less) and metadata that describes the file, in a single request.使用此上传类型可在单个请求中快速传输小文件(5 MB 或更少)和描述文件的元数据。 To perform a multipart upload, refer to Perform a multipart upload.要执行分段上传,请参阅执行分段上传。

    • When you want to upload the data of the large size, please use the resumable upload.当您要上传大尺寸的数据时,请使用可续传。 Ref 参考

References:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM