用lambda生成的预设URL在S3存储桶上托管的文件的用户下载

发布于 2025-02-13 17:42:14 字数 587 浏览 0 评论 0原文

我正在尝试拥有一个用户输入某个字符串的网站,并且基于该字符串,我返回一个匹配文件供用户下载。目前,我已经设置了它,以便有一个可以进行匹配的lambda函数,并且网页通过API网关与Lambda功能进行了对话。 lambda函数的结果是动态生成的预先定义URL,有效期为一个小时。

到目前为止,我的代码下载了一个文件(filename.json),该文件是一行,只包含预先符号的URL(引号),然后我必须打开该文件以获取链接,然后手动将其放在我的浏览器中。有没有办法修改此代码直接下载S3托管文件?我删除了确切的链接,并用********替换了一部分。

fetch("https://********.execute-api.us-east-1.amazonaws.com/test", requestOptions)
.then((res) => { return res.blob(); })
.then((data) => {
  var a = document.createElement("a");
  a.href = window.URL.createObjectURL(data);
  a.download = "FILENAME";
  a.click();
})

I'm trying to have a website where the user inputs a certain string, and based on that string I return a matching file for the user to download. Currently I have it set up so that there is a lambda function that does the matching, and the webpage talks to the lambda function via API gateway. The result of the lambda function is a dynamically generated presigned URL which is valid for one hour.

So far I have code that downloads a file (FILENAME.json) which is one line and just contains the presigned URL (in quotes), which I would then have to open to get a link and then manually place it in my browser. Is there a way to modify this code to directly download the S3-hosted file? I've removed the exact link and replaced a part of it with ********.

fetch("https://********.execute-api.us-east-1.amazonaws.com/test", requestOptions)
.then((res) => { return res.blob(); })
.then((data) => {
  var a = document.createElement("a");
  a.href = window.URL.createObjectURL(data);
  a.download = "FILENAME";
  a.click();
})

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ˉ厌 2025-02-20 17:42:14

我过于复杂的事情。以下作用很好:

    response = fetch("https://******.execute-api.us-east-1.amazonaws.com/test", requestOptions)
  .then(res => res.json())
  .then(data => obj = data)
  .then(obj => window.open(obj, "_blank"))
}

I was overcomplicating things. The following works just fine:

    response = fetch("https://******.execute-api.us-east-1.amazonaws.com/test", requestOptions)
  .then(res => res.json())
  .then(data => obj = data)
  .then(obj => window.open(obj, "_blank"))
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文