我想知道是否可以在公共网站上访问/显示存储在谷歌驱动器中的图像等文件。
当前回答
功能名称: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}
功能名称: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
这是唯一的直接链接格式没有重定向(然后一个永久链接),只适用于文件直接可见的谷歌驱动器(例如图像和文档)。 它不受下载限制的影响,您可以使用它在网站上显示图像。
例如共享链接:
https://drive.google.com/file/d/FILE_ID/view?usp=sharing
就变成:
https://lh3.googleusercontent.com/d/FILE_ID
Lh4, lh5和lh6也有作用。
我发现了一个非常有用的api/url:
imageId ->文件ID Width,期望的宽度(不能大于图像分辨率宽度),必须为整数才能工作 高度,期望的高度(不能大于图像分辨率高度),必须为整数才能工作
https://drive.google.com/thumbnail?id=${imageId}&sz=w${width}-h${height}
注意:api/url将保持纵横比,因此它将在第一个维度停止
在谷歌驱动器API中有一个文件类型选项。你可以检查它是否被解析成一个有效的图像。我会看一个选项,如果文件类型给我一个无效的图像,然后为文件获取一个新的直接URL。我还没有弄清楚如何做到这一点,但也许这是一个尝试的途径。