如何通过Google云端硬盘链接流式传输我应用中的音乐并使链接可访问?

问题描述

在网站上,用户可以上传音乐。这些音乐正在我的Google云端硬盘帐户中上传。之后,将以下链接格式写入数据库

https://drive.google.com/uc?id=ID_GOES_HERE

现在,当您访问链接时,它会发生如下变化:

https://doc-14-8s-docs.googleusercontent.com/docs/securesc/m4p0h6h3bpkoufgbajruvng9n4f51clu/fnjf7ftnl18svtcblses77hapi91jpe5/1597565925000/10139445714759083419/10139445714759083419/1mLu_myEgts9eaoWv6S9nxu8n4qfZM0Rr?authuser=0

长链接数据库中的链接,用于应用程序播放音乐,但是我没有权限再在那里打开它。如何使最后一个链接长链接可访问?有什么办法可以公开此长链接

谢谢!

    <form id="form">
  <input name="file" id="uploadfile" type="file">
  <input name="filename" id="filename" type="text">
  <input id="submit" type="submit">
</form>
<script>
const form = document.getElementById('form');
form.addEventListener('submit',e => {
  e.preventDefault();
  const file = form.file.files[0];
  const fr = new FileReader();
  fr.readAsArrayBuffer(file);
  fr.onload = f => {
    
    const url =   "https://script.google.com/macros/s/###/exec";  // <---    Please set the URL of Web Apps.

    const qs = new URLSearchParams({filename:    form.filename.value || file.name,mimeType: file.type});
fetch(`${url}?${qs}`,{method: "POST",body: JSON.stringify([...new Int8Array(f.target.result)])})
.then(res => res.json())
.then(e => console.log("https://drive.google.com/uc?   export=download&id=" + e.fileId))
    .catch(err => console.log(err));
  }
});
</script>

这是Google应用程序脚本

function doPost(e) {
  const folderId = "root";  // Folder ID which is used for   putting the file,if you need.

  const blob =   Utilities.newBlob(JSON.parse(e.postData.contents),e.parameter.mimeType,e.parameter.filename);
  const file = DriveApp.getFolderById(folderId ||  "root").createFile(blob);
  const responSEObj = {filename: file.getName(),fileId:  file.getId(),fileUrl: file.getUrl()};
  return   ContentService.createtextoutput(JSON.stringify(responSEObj  )).setMimeType(ContentService.MimeType.JSON);
}

-------编辑-------

我尝试了给定的解决方案:

不幸的是,它无法按预期工作。.我正在获得以下链接

https://drive.google.com/uc?export=download&id=1Tnh8UgAzFKrp6e-8QCl21v16L3yVCqso 

现在,当我从中获取ID并将其插入此处时

http://docs.google.com/uc?export=open&id=your_file_id 

它的结果是一个非常长的链接,这是我需要的链接。例如,如果我在私有浏览器模式下,则此人仍无法访问,它迫使我登录不需要的人。应该可以通过您的解决方案中的链接访问它。这是链接

https://doc-08-8s-docs.googleusercontent.com/docs/securesc/m4p0h6h3bpkoufgbajruvng9n4f51clu/cgag5dus6c18p9hc1hv0362urudm2g6t/1597730475000/10139445714759083419/10139445714759083419/1Tnh8UgAzFKrp6e-8QCl21v16L3yVCqso?e=open&authuser=0 

如何使此链接可访问?

链接似乎具有您已设置的http://docs.google.com/uc?export=open&id=your_file_id权限,但是如果您复制长链接,则此链接不提供权限设置,但仍会阻止。

解决方法

在创建文件后,您需要给它一个适当的access level来决定谁可以访问它,还需要给一个permission level来决定他们可以使用该文件做什么(读/写/删除)

对于您的用例,应该可以通过链接和只读方式对其进行公开访问。因此,这就是您可以在应用脚本中执行的操作

const file = DriveApp.getFolderById(folderId ||  "root").createFile(blob);
file.setSharing(DriveApp.Access.ANYONE_WITH_LINK,DriveApp.Permission.VIEW); // <-- add this