Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何为存储在Google Drive上的文件设置图像缩略图_Python_Google Drive Api - Fatal编程技术网

Python 如何为存储在Google Drive上的文件设置图像缩略图

Python 如何为存储在Google Drive上的文件设置图像缩略图,python,google-drive-api,Python,Google Drive Api,使用Google Drive API,我可以使用files.update()方法更新任何可写文件的属性: import datetime data = {'modifiedTime': datetime.datetime.utcnow().isoformat() + 'Z'} drive_service.files().update(fileId=file_id, body=data, fields='id').execute() 假设我有一张PNG图片发布在网站的某个地方:'image':'

使用Google Drive API,我可以使用
files.update()
方法更新任何可写文件的属性:

import datetime
data = {'modifiedTime': datetime.datetime.utcnow().isoformat() + 'Z'}
drive_service.files().update(fileId=file_id, body=data, fields='id').execute()
假设我有一张PNG图片发布在网站的某个地方:
'image':'http://pngimg.com/uploads/eagle/eagle_PNG1228.png“
或保存在本地驱动器上的png文件”“/Users/username/Downloads/my_image.png”

如何将已发布在web上的PNG文件或保存在本地驱动器上的PNG文件设置为存储在Google drive上的文件的缩略图

这是我到目前为止试过的。 在本地驱动器上保存了
source.png
文件后,我继续使用此代码将其编码为
base64\u encoded.png
文件(我不确定是否正确):

我把这个
base64\u encoded.png
文件上传到谷歌硬盘。我复制了它的URL地址:
https://drive.google.com/open?id=1234567890abcdefgh

我将此URL地址设置为Google Drive上文件的
缩略图
元数据属性:

metadata =  { "contentHints": { 'thumbnail':{'image':'https://drive.google.com/open?id=1234567890abcdefgh'}}}
result = drive_service.files().update(fileId='abcdefgh1234567', body=metadata).execute()
但是我得到了一个错误:
…返回了“ByteString的无效值:https://drive.google.com/open?id=1234567“>


错误在哪里?

您的url不符合url安全Base64编码图像(请参阅)

尝试使用任何本地主机,然后上载图像,然后使用缩略图的路径。我找到了一个,如果这是url安全的Base64编码,您可以尝试您的url

已测试驱动器URL:

测试
www.localhost:8080/image/dog.png
URL


希望这有帮助。

这个示例脚本怎么样?
contenthipts.thumbnail.image
是URL安全的Base64编码图像。因此,要用作新缩略图的图像数据必须转换为URL安全的Base64编码数据。为此,它在Python中使用
base64.urlsafe\u b64encode()

更新缩略图也有一些限制。请在查看详细信息

我使用zip文件作为示例。Zip文件在谷歌硬盘上没有缩略图。当它确认使用
drive.files.get
时,
hasThumbnail
为false。所以这是可以使用的。虽然我使用这个脚本来谷歌文档和图像,但更新后的图像并没有反映给它们。这可能涉及到一些限制。将缩略图提供给zip文件时,
hasThumbnail
变为true。在我的环境中,在第一次更新时,更新有时会失败。但在那个时候,第二次更新运行良好。我不知道原因。对不起

示例脚本: 结果:


如果这对您没有用,我很抱歉。

感谢您指出拇指URL64不安全的事实。我将thumb上传到另一个web服务器。我用JSFIDLE检查了新的Thumb URL。它过去了。但是用于设置Google驱动器文件元数据的命令失败
meta={“contenthits”:{“缩略图”:{“图像”:https://www.safeurl.com/encoded.png“}}}
失败。
drive\u service.files().update(fileId=file\u id,body=meta)。execute()
失败,并显示相同的错误消息:
googleapiclient.errors.HttpError:
metadata =  { "contentHints": { 'thumbnail':{'image':'https://drive.google.com/open?id=1234567890abcdefgh'}}}
result = drive_service.files().update(fileId='abcdefgh1234567', body=metadata).execute()
import base64  # Use this

with open("./sample.png", "rb") as f:
    metadata = {
        "contentHints": {
            "thumbnail": {
                "image": base64.urlsafe_b64encode(f.read()).decode('utf8'),
                "mimeType": "image/png",
            }
        }
    }
    res = drive_service.files().update(
        body=metadata,
        fileId="### file ID ###"
    ).execute()
    print(res)