'How to set image thumbnail for file stored on Google Drive
Using Google Drive API I can update any 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 say I have a PNG image posted somewhere on a web: 'image':'http://pngimg.com/uploads/eagle/eagle_PNG1228.png'
or a png file that was saved on a local drive "/Users/username/Downloads/my_image.png"
.
How to set a PNG file already posted on a web or a PNG file saved on a local drive as the thumbnail image for the file that is stored on Google Drive?
Here is what I tried so far.
With the source.png
file saved on my local drive I went ahead and encoded it to base64_encoded.png
file using this code (I am not sure if I did it 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 this base64_encoded.png
file to Google Drive. I copied its URL address: https://drive.google.com/open?id=1234567890abcdefgh
.
I set this URL address as a thumbnail
metadata property of the 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 I am getting an error: ...returned "Invalid value for ByteString: https://drive.google.com/open?id=1234567">
Where is the mistake?
Solution 1:[1]
How about this sample script?
contentHints.thumbnail.image
is URL-safe Base64-encoded image. So an image data that you want to use as new thumbnail has to be converted to URL-safe Base64-encoded data. For this, it uses base64.urlsafe_b64encode()
at Python.
And there are some limitations for updating thumbnail. Please check the detail information at https://developers.google.com/drive/api/guides/file#upload_thumbnails.
I used zip file as a sample. Zip file doesn't have the thumbnail on Google Drive. When it confirms using drive.files.get
, hasThumbnail
is false. So this can be used. Although I used this script to Google Docs and images, the updated image was not reflected to them. This may touch to the limitations. When the thumbnail is given to the zip file, hasThumbnail
becomes true. In my environment, at the first update, the update had sometimes been failed. But at that time, the second update had worked fine. I don't know about the reason. I'm sorry.
Sample script :
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)
Result :
If this was not useful for you, I'm sorry.
Solution 2:[2]
Your url is not compliant to URL-safe Base64-encoded image (see RFC 4648 section 5)
Try using any localhost then upload your image then use the path for the thumbnail. I've found a jsfiddle, where you can try your url if this is URL-safe Base64-encoded.
Tested your drive URL:
Tested www.localhost:8080/image/dog.png
URL
Hope this helps.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Richie Bendall |
Solution 2 | Community |