3

I am trying to write code to get the direct urls of images in a public Google Drive folder and embed them. Until a few minutes ago everything has been working and using the uc? trick gives the raw image so I can embed it. However now it is sending a download, even when I use &export=view.

Here is my code:

key = "supersecretgooglecloudconsolekey"

import re
import requests

def get_raw_image_links(folder_link):
    # Extract folder id
    folder_id = re.findall(r"/folders/([^\s/]+)", folder_link)[0]

    # Google cloud console request to get links
    api_url = f"https://www.googleapis.com/drive/v3/files?q='{folder_id}'+in+parents+and+mimeType+contains+'image/'&key={key}"
    response = requests.get(api_url).json()

    raw_image_links = []

    # Iterate over the files and retrieve raw image links
    for file in response['files']:
        raw_link = f"https://drive.google.com/uc?id={file['id']}"
        print(raw_link)
        raw_image_links.append(f'<a href="{raw_link}"> <img src="{raw_link}" /> </a>')

    return ''.join(raw_image_links)


folder_link = "https://drive.google.com/drive/u/0/folders/**********"
image_links = get_raw_image_links(folder_link)



For example, this works: https://drive.google.com/uc?id=15bwLlBuPc4O8LXwxjsBJe_ctyPl8Tll2

But this doesn't for some reason and instead sends a download: https://drive.google.com/uc?id=1Gz4YrH2tefSVxKRvv6gnqnY3tk0Obm0T

How do I fix this? Also, it would be helpful if anyone knows how to scrape the raw image link from the viewing page of the image itself https://drive.google.com/file/d/15bwLlBuPc4O8LXwxjsBJe_ctyPl8Tll2/view

Thank you!

433MEA
  • 100
  • 8

2 Answers2

3

In your situation, how about changing the endpoint? When your showing script is modified, please modify raw_link as follows.

From:

raw_link = f"https://drive.google.com/uc?id={file['id']}"

To:

raw_link = f"https://drive.google.com/thumbnail?id={file['id']}&sz=w1000" # Please modify w1000 to your actual situation.
  • By this change, the image files are converted to PNG format. I thought that this might be one of several solutions.
  • From your question and showing script, I guessed that your folder of folder_link = "https://drive.google.com/drive/u/0/folders/**********" might be publicly shared. In that case, I thought that the above modification might be able to be used.

Reference:

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • @433MEA Thank you for replying and testing it. I'm glad your issue was resolved. Thank you, too. – Tanaike Jun 25 '23 at 23:00
1

The reason why you cannot preview https://drive.google.com/uc?id=1Gz4YrH2tefSVxKRvv6gnqnY3tk0Obm0T is related to the HEIC format, we can store HEIC files in Google Drive but we cannot preview them, unfortunately the feature is not included yet.

Here's the list of image files you can preview: Image files (.JPEG, .PNG, .GIF, .BMP, .TIFF, .SVG). There's no option for us to preview HEIC image files, if you can spare the time, please consider submitting a Feature idea for further enhancements to Google Drive.

Reference:

Samuel Lopez
  • 116
  • 5