python - How to set image thumbnail for file stored on Google Drive -
using google drive api can update writeable file's attribute using files.update()
method:
import datetime data = {'modifiedtime': datetime.datetime.utcnow().isoformat() + 'z'} drive_service.files().update(fileid=file_id, body=data, fields='id').execute()
lets have png image posted somewhere on web: 'image':'http://pngimg.com/uploads/eagle/eagle_png1228.png'
or png file saved on local drive "/users/username/downloads/my_image.png"
.
how set png file posted on web or png file saved on local drive thumbnail image file stored on google drive?
here tried far. source.png
file saved on local drive went ahead , encoded base64_encoded.png
file using code (i not sure if did correctly):
import base64 src = '/source.png' dst = '/base64_encoded.png' image = open(src, 'rb') image_read = image.read() encodestring = base64.encodestring(image_read) image_result = open(dst, 'wb') image_result.write(encodestring)
i uploaded base64_encoded.png
file google drive. copied url address: https://drive.google.com/open?id=1234567890abcdefgh
.
i set url address thumbnail
metadata property of file on google drive:
metadata = { "contenthints": { 'thumbnail':{'image':'https://drive.google.com/open?id=1234567890abcdefgh'}}} result = drive_service.files().update(fileid='abcdefgh1234567', body=metadata).execute()
but getting error: ...returned "invalid value bytestring: https://drive.google.com/open?id=1234567">
where mistake?
how sample script? contenthints.thumbnail.image
url-safe base64-encoded image. image data want use new thumbnail has converted url-safe base64-encoded data. this, uses base64.urlsafe_b64encode()
@ python.
and there limitations updating thumbnail. please check detail information @ https://developers.google.com/drive/v3/web/file#uploading_thumbnails.
i used zip file sample. zip file doesn't have thumbnail on google drive. when confirms using drive.files.get
, hasthumbnail
false. can used. although used script google docs , images, updated image not reflected them. may touch limitations. when thumbnail given zip file, hasthumbnail
becomes true. in environment, @ first update, update had been failed. @ time, second update had worked fine. don't know reason. i'm sorry.
sample script :
import base64 # use open("./sample.png", "rb") 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)
result :
if not useful you, i'm sorry.
Comments
Post a Comment