我想知道是否可以在公共网站上访问/显示存储在谷歌驱动器中的图像等文件。
当前回答
我发现了一个非常有用的api/url:
imageId ->文件ID Width,期望的宽度(不能大于图像分辨率宽度),必须为整数才能工作 高度,期望的高度(不能大于图像分辨率高度),必须为整数才能工作
https://drive.google.com/thumbnail?id=${imageId}&sz=w${width}-h${height}
注意:api/url将保持纵横比,因此它将在第一个维度停止
其他回答
功能名称: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
如果你想在浏览器中查看文件,也可以使用类似于rufo和Torxed提供的方法:
https://drive.google.com/uc?export=view&id={fileId}
Vetea,如果你从图片URL中获取链接,它不工作,但如果你从“直接链接”字段中获取它,它应该工作。我在多个场合使用并测试过它。
编辑:截至2020年,这是有效的。以前的大多数答案都过时了。
简单的解决方案
你所要做的就是打开你的文件:
然后,进入你的网页检查器(Chrome, Cmd-Shift-I或Ctrl-Shift-I取决于你的操作系统),并获得链接。将该链接粘贴到浏览器中,它将重定向到另一个链接。复制新的URL。完成了!
重定向的目的是什么?
似乎如果你使用第一个链接,它只能在登录到你的谷歌帐户时才能访问。对其他人没有多大帮助。然而,第二个重定向链接不需要您登录。这就是它背后的基本原理。
我删除了图像中显示的原始文件,但这里有另一个工作示例。
实际上,我已经检查了大约一周前在我的编辑中发布的示例链接,但它似乎不再工作了。看起来这些链接只是临时工作,所以不要在任何生产环境中使用它们。
一个解决方法是使用谷歌Drive SDK API获取fileId,然后使用这个Url:
https://drive.google.com/uc?export=view&id= {id}
这将是一个永久链接到您的文件在谷歌驱动器(图像或其他任何东西)。
注:此链接似乎受配额限制。所以不适合公开/大规模共享。