我想知道是否可以在公共网站上访问/显示存储在谷歌驱动器中的图像等文件。


当前回答

2022年11月面向工作空间(G套件)用户的工作解决方案

一些嵌入代码不显示img,除非查看者登录到gmail,下面的工作就像你的网络主机上的常规图像:

公开分享项目(确保它说“互联网上任何有此链接的人都可以查看):要么图片本身(或图片所在的文件夹),然后按以下方式复制链接

获取文件的ID并在这里插入:

<img src="https://lh3.googleusercontent.com/d/[ID_OF_THE_FILE]">

结果(如果你看到下面的狗,这意味着解决方案仍然有效):

Localmachine

不会显示在MAMP/WAMP类型的本地主机环境,但显示在网页上。

其他回答

一个解决方法是使用谷歌Drive SDK API获取fileId,然后使用这个Url:

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

这将是一个永久链接到您的文件在谷歌驱动器(图像或其他任何东西)。

注:此链接似乎受配额限制。所以不适合公开/大规模共享。

如果你想在浏览器中查看文件,也可以使用类似于rufo和Torxed提供的方法:

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

1.将您的图片设置更改为公共。

2.获取其可共享链接。

3.去这个网站并生成一个直接下载链接。

为我工作!

功能名称:goobox

标签:图像托管,regex, URL,谷歌驱动器,dropbox,高级

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 

在谷歌驱动器API中有一个文件类型选项。你可以检查它是否被解析成一个有效的图像。我会看一个选项,如果文件类型给我一个无效的图像,然后为文件获取一个新的直接URL。我还没有弄清楚如何做到这一点,但也许这是一个尝试的途径。