简体   繁体   English

在我的 Angular 应用程序中使用 S3 预签名 url 下载多个文件

[英]Download multiple files using S3 pre-signed url in my Angular Application

I am making REST API call to get an array of pre-signed URLs from S3.我正在调用 REST API 以从 S3 获取一组预签名的 URL。 These URL are files that could be XML, CSV, JSON etc.这些 URL 是可以是 XML、CSV、JSON 等的文件。

How do I loop download files from these URLs without opening a new tab?如何在不打开新标签的情况下从这些 URL 循环下载文件? I do not want to use AWS SDK for NodeJS to avoid tight coupling with my front-end.我不想使用 AWS SDK for NodeJS 来避免与我的前端紧密耦合。 Application currently has Angular 7, NodeJS and ExpressJS.应用程序目前有 Angular 7、NodeJS 和 ExpressJS。

getFile(url, params){
this.awsservice.getFile(url, params).subscribe(
  (response) => {
    const res = JSON.parse(JSON.stringify(response));
    var apiList = [];

    for (var key in res) {
      if(key == 'api'){
        apiList = res[key];
      }
    }

    apiList.forEach(function (url) {

  // Logic to download file
  document.location.assign(url) //Only seems to download the last file in the array
      console.log("Download started: "+url);
  });

  },
  (error) => {
    this.tempErrorFlag = true;
    const errorMsg = error;
    console.log(`ERROR ::: reInitiate API ::: ${errorMsg.message}`);
  });
}

I tried adding document.location.assign(url) but it only seems to download the last url in the array.我尝试添加document.location.assign(url)但它似乎只下载了数组中的最后一个 url。 Even adding delay didn't help.即使增加delay也无济于事。

Appreciate your help.感谢你的帮助。

I was able to download the files but not in the same tab.我能够下载文件,但不能在同一个选项卡中。 First I fetch presigned URLs from my angular service, which I feed into an array.首先,我从我的 Angular 服务中获取预签名的 URL,并将其输入到一个数组中。 Then basically with each of the presigned urls, I create a temporary anchor tag to and assign href the presigned url value.然后基本上使用每个预签名的 url,我创建一个临时锚标记并分配 href 预签名的 url 值。 The snackbarService is for showing a popup when the download begins. snapbarService 用于在下载开始时显示一个弹出窗口。 The entire code looks like below:整个代码如下所示:

  downloadItem() {
  let urls = [];
  for(let item of this.selectedRowsData) {
  //calling the service to fetch the presigned url
  this.dataService.getPresignedToDownloadAll(
    item.value,
    item.id).subscribe((res) => {
      urls.push(res);
      this.download(urls);
   });
 }
}

download(urls: any) {
  var self = this;
  var url = urls.pop();
  setTimeout(function(){
    self.snackBarService.loadComponent({
      isSuccess: true,
      message: MESSAGES.downloadInProgress,
    });
  var a = document.createElement('a');
  a.setAttribute('href', url);
  document.body.appendChild(a);
  a.setAttribute('download', '');
  a.setAttribute('target', '_blank');
  a.click();
 // a.remove();
 }, 1000)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法使用 JS 从 S3 预签名 url 下载多个文件 - Not able to download multiple files from S3 pre-signed urls using JS 如何使用 angular 或 javascript 中的预签名 url 将文件上传到 S3 存储桶 - how to upload file to S3 bucket using pre-signed url in angular or javascript 如何使用我的签名证书生成预签名 URL 以在 Amazon S3 上提出异议 - How to use my signed-certificate to generate pre-signed URL to object at Amazon S3 从没有弹出窗口阻止程序的预签名 URL 下载 S3 文件 - Download S3 file from pre-signed URL without popup blocker 如何使用后端预签名URL使用其SDK将文件上传到Amazon S3? - How can i use a backend pre-signed url to upload files to Amazon S3 using its SDK? S3使用预先签名的URL从浏览器上传图片 - S3 Upload image with pre-signed url from browser 使用angular + aws-sdk-js +预签名网址将文件上传到S3 - Uploading a file to S3 with angular + aws-sdk-js + pre-signed url 通过预签名URL将PUT上传到S3时拒绝签名 - Signature Denied in PUT upload to S3 with Pre-Signed URL 如何使用预签名的 URL 而不是凭证直接从浏览器上传到 AWS S3? - How to upload to AWS S3 directly from browser using a pre-signed URL instead of credentials? AWS S3 使用预签名的 URL 更新映像(Axios-PUT 请求) - AWS S3 update image using pre-signed URL (Axios-PUT Request)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM