user1355487
user1355487

Reputation: 3959

Displaying files (e.g. images) stored in Google Drive on a website

I was wondering if its possible to access/display files like images which are stored in Google Drive on a public website.

Upvotes: 395

Views: 584370

Answers (26)

Robert Sinclair
Robert Sinclair

Reputation: 5406

Working solution for Workspace (G suite) users in Oct 2024

Some embed codes do not show the img unless the viewer is logged into gmail, the following works like a regular image on your webhost:

Share item publicly (make sure it says ''Anyone on the internet with this link can view): either the image itself (or the folder where the image resides) then copy the link the following way

enter image description here

Take the ID of the file and insert here:
<img src="https://lh3.googleusercontent.com/d/[ID_OF_THE_FILE]">
Result (if you see the dog below it means the solution still works):

Localmachine

Won't display on MAMP/WAMP type of localhost environments, but does display on webpages.

Upvotes: 72

Lorenz Lo Sauer
Lorenz Lo Sauer

Reputation: 24690

UPDATE: As was announced, Google deprecated this feature in Aug 2016. Here's the final update from Google with alternatives.

As per April 2013 and using Chrome/webkit, the following worked for me:

  1. Make a folder called e.g. "public"
  2. Select that folder, right click and Share > Share. Click. Select "Anyone can access"
  3. Drag and Drop a file into the folder, and let it upload.
  4. Right click on the file and select Details. One of the lines in the Details-Fieldset reads "Hosting". Underneath it is an url:

https://googledrive.com/...

  • Drag and Drop that url into a new tab. Copy and paste the url and share or embed it anywhere you like.

One limitation is that as far as HTTP goes, only secure HTTP access seems to be possible.

Update:
Another limitation is that files which Google drive can open, won't be accessible that way.
That is, clicking on "Details" won't show an Google-drive url.

To overcome this:

  • right click on the file in question and select "Open with>Manage apps":

enter image description here

  • Untick the file-associated apps here
  • Optional: Reload Google Drive
  • Right click on the file and select "Details"
  • Proceed as in step #4

Note: An alternative to the procedure above, is uploading the file with an extension that Google Drive cannot open/is not associated.

Upvotes: 24

Mori
Mori

Reputation: 6770

  1. Go to your Google Drive.
  2. Right-click on the image file.
  3. Select Share.
  4. Open the General access drop-down menu.
  5. Select Anyone with the link to publish your image.
  6. Click Copy link and then Done.

You will get a URL like the following:
https://drive.google.com/file/d/1wMgCWAsqlw0nXcMhCldTbwSznMdXUmBT/view?usp=sharing

You can see your file ID in bold. Put the file ID in the following link:
https://drive.usercontent.google.com/download?id=FILE ID

Result:
https://drive.usercontent.google.com/download?id=1wMgCWAsqlw0nXcMhCldTbwSznMdXUmBT

Upvotes: 268

Philippe
Philippe

Reputation: 31097

Having tested a lot of url different formats, the one working the best is https://lh3.googleusercontent.com/d/{FILE_ID} (lh3 could be replaced from lh2 to lh6) which not require to be logged with a Google account as long as the file has the permission "Anyone with the link" set to "Viewer".

Below a solution to get the shared link for a lot of files (of a given Gdrive folder)

Summary: automatically fill a Google spreadsheet file with for each files/images of a given folder the public share link

Building on @robert-sinclair solution (the only one working well for me)...

Steps:

  1. Create a Google sheet file and rename the 1st sheet to Files (Or at least keep the sheet name and the value of the sheetName variable in sync.)
  2. Go in menu "Extensions" => "Apps script":
  3. Paste and adapt the given script (at least the folderName value):
const folderName =  "My folder name"; // <== ToDo(edit): change to fit your needs

const sheetName =  "Files";
const headers = ["Files/Photos", "Photo Urls", "Folder", "Photo Url 2", "Id", "Drive Url"];

function getAllFilesFromMyFolder() {
 const photoFolder = DriveApp.getFoldersByName(folderName).next();
 const app = SpreadsheetApp.getActive(); // get current spreadsheet file
 const sheet = app.getSheetByName(sheetName); // and the right sheet
 sheet.clear(); // /!\ clear previous data from sheet
 let rows = [];

 getFolderFiles(photoFolder, rows);
 //Add subfolders files (only one level.)
 const subFolders = photoFolder.getFolders();
 while(subFolders.hasNext()) {
   var subFolder = subFolders.next();
   if(subFolder != null) {
      getFolderFiles(subFolder, rows);
   }
 }

 rows = rows.sort((a, b) => a[0].localeCompare(b[0])); // sort files by name (not mandatory)
 rows.unshift(headers); // add a header (not mandatory)
 sheet.getRange(1, 1, rows.length, headers.length).setValues(rows);
}

function getFolderFiles(folder, rows) {
 const myFiles = folder.getFiles(); // get all files. Maybe prefer `getFilesByType(mimeType)`. See https://developers.google.com/apps-script/reference/drive/folder
 while(myFiles.hasNext()) {
   const file = myFiles.next();
   if(file != null) {
     const fileId = file.getId();
     rows.push([file.getName(), `https://lh3.googleusercontent.com/d/${fileId}`, folder.getName(), `https://drive.google.com/uc?id=${fileId}`, fileId, file.getUrl()]);
   }
 }
}

  1. Select the function getAllFilesFromMyFolder to run, save and run the script:

select script and run

  1. The google sheet will be populated with all the files and sub folder files of the folder filled in folderName variable. Use the 2nd column "Photo Urls" values.

Upvotes: 0

ctf0
ctf0

Reputation: 7579

here is how from @ https://productforums.google.com/forum/#!topic/drive/yU_yF9SI_z0/discussion

1- upload ur image

2- right click and chose "get sharable link"

3- copy the link which should look like

https://drive.google.com/open?id=xxxxxxx

4-change the open? to uc? and use it like

<img src="https://drive.google.com/uc?id=xxxxx">

Upvotes: 79

Mario Palumbo
Mario Palumbo

Reputation: 985

This is the only direct link format without redirection (then a Permalink) and only applies to files directly visible on Google Drive (e.g. images and documents).
It is not affected by the download limit and you can use it to display images on a website.

For example the sharing link:

https://drive.google.com/file/d/FILE_ID/view?usp=sharing

becomes:

https://lh3.googleusercontent.com/d/FILE_ID

lh4, lh5 and lh6 also work.

Upvotes: 7

applemonkey496
applemonkey496

Reputation: 743

EDIT : As of 2020, THIS is working. Most previous answers are outdated.

Easy Solution

All you have to do is open your file:

Image of mountain on Google Drive

Then, go into your web inspector (for Chrome, Cmd-Shift-I or Ctrl-Shift-I depending on your OS) and get the link. Paste that link into your browser and it will redirect to another link. Copy the new URL. Done!

Image of link for image shown in web inspector


What's the redirect for?

It seems that if you use the first link, it can only be accessed when signed in to your Google account. Not very helpful for other people. The second, redirected link, however, does not need you to be signed in. That's the rationale behind it.


I deleted the original file shown in the images, but I have another working example here.


I've actually checked back on my example link that I posted in my edit about a week ago, but it no longer seems to be working. It looks like these links only work temporarily, so don't use them for any kind of production environment.

Upvotes: 35

Let Me Tink About It
Let Me Tink About It

Reputation: 16102

Per this blog post, a currently working solution is:

<img src=”https://drive.google.com/uc?id=[imageIdGoesHere]" />

Test here

https://drive.google.com/uc?id=1m-uOoFzHn4oUGlEsDSEfPBbJ2QhBJzlM

This is verified to work as of 26th of April, 2021. No shared folder or login is required. But a publicly shared file is.

Upvotes: 8

Demotte
Demotte

Reputation: 713

You can follow below steps to embed the files you want to your website.

  1. Find the PDF file in Google Drive
  2. Preview the PDF file in Google Drive
  3. Pop-out the Google Drive preview
  4. Use the More actions menu and choose Embed item
  5. Copy code provided
  6. Edit Google Sites page where you want to embed
  7. Open the HTML Editor
  8. Paste the HTML embed code provided by the Google Drive preview
  9. Use the Update button and Save the page

References: https://www.steegle.com/websites/google-sites-howtos/embed-drive-pdf

Upvotes: 1

charri
charri

Reputation: 1052

A very useful api/url I found is:

  • imageId -> the file ID
    screenshot of where you can find fileId if you're struggling
  • width, the desired width (cannot be greater than image resolution width), must be integer for it to work
  • height, the desired height (cannot be greater than image resolution height), must be integer for it to work
https://drive.google.com/thumbnail?id=${imageId}&sz=w${width}-h${height}

Note: the api/url will keep the aspect ratio so it will stop at whichever dimension is met first

Upvotes: 3

Ahmad Ali
Ahmad Ali

Reputation: 774

Function title: goobox

tags: image hosting, regex, URL, google drive, dropbox, advanced

  • return: string, Returns a string URL that can be used directly as the source of an image.
  • when you host an image on google drive or dropbox you can't use the direct URL of your file to be an image source.
  • you need to make changes to this URL to use it directly as an image source.
  • goobox() will take the URL of your image file, and change it to be used directly as an image source.
  • Important: you need to check your files' permissions first, and whether it's public.
  • live example: https://ybmex.csb.app/
cconst goobox = (url)=>{

    let dropbox_regex = /(http(s)*:\/\/)*(www\.)*(dropbox.com)/;
    let drive_regex =/(http(s)*:\/\/)*(www\.)*(drive.google.com\/file\/d\/)/;

    if(url.match(dropbox_regex)){
       return url.replace(/(http(s)*:\/\/)*(www\.)*/, "https://dl.");
    }
    if(url.match(drive_regex)){
        return `https://drive.google.com/uc?id=${url.replace(drive_regex, "").match(/[\w]*\//)[0].replace(/\//,"")}`;
    }
    return console.error('Wrong URL, not a vlid drobox or google drive url');
}
let url = 'https://drive.google.com/file/d/1PiCWHIwyQWrn4YxatPZDkB8EfegRIkIV/view' 

goobox(URL); //  https://drive.google.com/uc?id=1PiCWHIwyQWrn4YxatPZDkB8EfegRIkIV 

Upvotes: 0

y2k-shubham
y2k-shubham

Reputation: 11597

Specifically for G-Suite users . As reported in point 3 here, you can use this URL for hosting image

https://drive.google.com/a/domain.com/thumbnail?id=imageID

with following replacements

  • domain: replace with your company's GSuite domain like pikachu
  • imageID: replace with the image id / file id

The prerequisite here is that image should have been shared via drive to target audience (either with each person individually or maybe across the org)

enter image description here


If you face problems with size of rendered image, use following options as mentioned here

https://drive.google.com/a/domain.com/thumbnail?id=imageID&sz=w{width}-h{height}

with following replacements (in addition to domain and imageID replacement)

  • {width}: write the width in pixels (without braces) like 300
  • {height}: write the height in pixels (without braces) like 200

Upvotes: 9

x89
x89

Reputation: 3450

1.Change the settings of your picture to public.

2.Get its shareable link.

3.Go to this website and generate a direct download link.

Worked for me!

Upvotes: 1

niutech
niutech

Reputation: 29922

UPDATE: As was announced, Google deprecated this feature in Aug 2016. Here's the final update from Google with alternatives.

Yes, it's possible. Provided that you put your files in a public folder, you can get any file in a folder by this URL:

https://googledrive.com/host/<folderID>/<filename>

Upvotes: 166

rufo
rufo

Reputation: 5527

A workaround is to get the fileId with Google Drive SDK API and then using this Url:

https://drive.google.com/uc?export=view&id={fileId}

That will be a permanent link to your file in Google Drive (image or anything else).

Note: this link seems to be subject to quotas. So not ideal for public/massive sharing.

Upvotes: 264

Sagar Munjal
Sagar Munjal

Reputation: 774

However this answer is simple, infact very simple and yea many have mentioned it that simply put the id of image into the following link https://drive.google.com/uc?export=view&id={fileId}

But however easy that is, I made a script to run in the console. Feed in an array of complete sharable links from google drive and get them converted into the above link. Then they can be simply used as static addresses.

array = ['https://drive.google.com/open?id=0B8kNn6zsgGEtUE5FaGNtcEthNWc','https://drive.google.com/open?id=0B8kNn6zsgGEtM2traVppYkhsV2s','https://drive.google.com/open?id=0B8kNn6zsgGEtNHgzT2x0MThJUlE']

function separateDriveImageId(arr) {
for (n=0;n<arr.length;n++){

str = arr[n]

for(i=0;i<str.length;i++){
if( str.charAt(i)== '=' ){
var num = i+1;
var extrctdStrng = str.substr(num)

}
}
console.log('https://drive.google.com/uc?export=view&id='+extrctdStrng)
window.open('https://drive.google.com/uc?export=view&id='+extrctdStrng,'_blank')
}


}
separateDriveImageId(array)

Upvotes: -2

Watchmaker
Watchmaker

Reputation: 5308

From google drive help pages:

To host a webpage with Drive:

  1. Open Drive at drive.google.com and select a file.
  2. Click the Share button at the top of the page.
  3. Click Advanced in the bottom right corner of the sharing box.
  4. Click Change....
  5. Choose On - Public on the web and click Save.
  6. Before closing the sharing box, copy the document ID from the URL in the field below "Link to share". The document ID is a string of uppercase and lowercase letters and numbers between slashes in the URL.
  7. Share the URL that looks like "www.googledrive.com/host/[doc id] where [doc id] is replaced by the document ID you copied in step 6.

Anyone can now view your webpage.

Upvotes: 6

user3642085
user3642085

Reputation: 67

Some of the previous users were close, but they were missing a step here or there.

Here is a video that shows all of the steps.

(Edit 2-Dec-14
The Below information is incorrect when it comes to the new Google Drive. For the New Google Drive follow these instructions.
There are two options you can use,
option 1 you can click the cog on the top right and revert to the old google drive, IF you revert, use the instructions after "End Edit)" .
Option 2 or you can follow the work around I found. If I find a better way than this I will update it, but here is what I have found that works.


The full link will look like this "https://googledrive.com/host/(folder id)
Part one of your link that you need is "https://googledrive.com/host/" for the second half you will need to navigate to the file you would like to share.
Once you are in the folder with the file you would like to share, look at the link above
(Example 1 https://drive.google.com/drive/u/0/#folders/0B3UALYkiLexYSXZlcldoU2NpYXM )
(Example 2 https://drive.google.com/drive/u/0/#folders/0B3UALYkiLexYSXZlcldoU2NpYXM/0B3UALYkiLexYRkNnOVhsUVozRU0)
In both of these above examples, the "Folder ID" you need for sharing is the last group of letters and numbers after the "/" so in example one, it is "0B3UALYkiLexYSXZlcldoU2NpYXM" in example two it is "0B3UALYkiLexYRkNnOVhsUVozRU0"

In the examples I used, example 1 was a folder on my drive, and example 2 was a folder inside that first one, that is why it has the entire first link before the second.
We only need the section after the "/" furthest to the right.


So now that you have your "Folder ID", take the above formula "https://googledrive.com/host/(folder id)"
Example 1 https://googledrive.com/host/0B3UALYkiLexYSXZlcldoU2NpYXM
Example 2 https://googledrive.com/host/0B3UALYkiLexYRkNnOVhsUVozRU0


Great, now that you have this link, open it in a new page. It will direct you to the shared folder. Once there you can either right click on any file and select "Copy link address" or you can click any file in that folder and it will take you to the hosted image, the URL at the top of the page is the hosting URL.


That is the how you do it. It is quite annoying, and personally it seems a whole lot easier to just revert to the old google drive.


I will try to make a new tutorial video ASAP


Let me know if this does not work for you and what problem you are experiencing.


End Edit)

https://www.youtube.com/watch?v=QmN22LMPdDk&feature=youtu.be

Or you can just follow the written ones below.

These pictures go with the ones listed in the steps.

https://googledrive.com/host/0B3UALYkiLexYSXZlcldoU2NpYXM/

  1. Create a Folder on your Google Drive that you would like to use for sharing images.

  2. Select that folder and go to the sharing options. Change the "Who has access" options from "Specific People" to "Public on the web" All images placed in folder will have a hosting link on them shown in Step 4

    (Images : Change Folder Option.png, Change folder option 2.png, and Change folder option 3.png)

  3. place an image in that folder.

  4. select the image you would like to share and look at the details section (usually on right hand side) for a section labeled "Hosting" you should find a link that starts with

    "googledrive.com/host/(random numbers and digits that are the ID for that folder)/(file name)"

    Use that link to share your images. You can use that link to embed them into other websites.

    (Images: Change folder option 4.png and Change folder option share.png)

Upvotes: 4

Vijay Rumao
Vijay Rumao

Reputation: 72

Solution provided by Niutech worked for me i.e.

http://googledrive.com/host/<folderID>/<filename>

But there are 2 outstanding issues

  1. You cannot have 2 files with the same name in the same folder in the drive else this link won't work.

  2. It is not yet clear but Google seems to be planning to deprecate image hosting via drive. please see the link below. https://support.google.com/richmedia/answer/6098968?hl=en

Upvotes: 0

T.Todua
T.Todua

Reputation: 56341

For images, open an image in google viewer. i.e.

Method 1

https://docs.google.com/file/d/0Bz7qe_olclTwWDNJRDRmb1pJamM/edit

then view source> find word "texmex-thumb" and aside it there will be link.

Upvotes: 1

Jessica Potter
Jessica Potter

Reputation: 19

You can do it directly from Drive & Gmail. Here's how:

1.Upload an image to Google drive and set permissions for viewing (can be public OR anyone w/ link)

  1. Go to Gmail>Compose. Select the + next to attachment icon.

  2. Select drive icon (triangle shape)

  3. Navigate to your image and right-click copy image url

  4. Paste into web browser or embed on webpages as needed.

Upvotes: 1

Jeff Johnson
Jeff Johnson

Reputation: 1097

If you want to view the file in the browser, it's also possible using a similar method to the one provided by rufo and Torxed:

https://drive.google.com/uc?export=view&id={fileId}

Upvotes: 18

binwilly
binwilly

Reputation: 5

Con can disable javascript in your browser open the image file and in the view page source or right click on the image, you will see the image link. ( check share preference before )

Upvotes: -2

Jarzan
Jarzan

Reputation: 96

Vetea, if you take the link from picture URL, it does not work, but if you take it from the field "Direct Link" it should work. I have used and tested it in multiple occasions.

Upvotes: 3

Sax
Sax

Reputation: 11

There is a filetype option in the Google Drive API. You could, maybe, check if that resolves to a valid image. I'd look at an option where if the filetype gives me an invalid image, then get a new direct URL for the file. I haven't figured out exactly how to do this though, but maybe that's a path to try.

Upvotes: 1

Nicolas Garnier
Nicolas Garnier

Reputation: 12374

I think it is possible but only for a short time

What you have to do is set the Access Control List of the file to Public Read-Only (or Public Read/Write). You can do that programmatically using the Google Document List API, or manually through the "Share" button on the Drive image viewer.

Then you can get the URL to the image programmatically by either using the Google Document List API or using the Google Drive API (i.e. file.getDownloadUrl() in Java). You can also easily get a link to the image manually by right clicking on the image in the Google Drive default image viewer.

The problem is that this link has a limited time to live, so it will work for a little while and then stop working.

Basically the URL of the image file stored in Drive should be accessible without any authentication once it has been set shared publicly but that URL is going to change at some point. We might find a solution to this in the future like providing a permanent URL that will redirect to these changing URL but no promises...

Upvotes: 7

Related Questions