Angular-Amazon S3-在下载时从AWS签名的URL更改文件名

我有一个从UI上传到S3的CSV文件( TestFile.CSV ),我想在另一个屏幕上下载相同的文件,但是我必须提供一个自定义文件名(例如, TestFile_username.CSV )供用户保存/下载到用户计算机上。

public downloadFile(key: any): Observable<string> {
let s3 = new AWS.S3();
let downloadUrl = s3.getSignedUrl('getObject',{ Bucket: environment.bucket,Key: key });
return of(downloadUrl);}

以上功能为我提供了签名的URL,并会自动以原始名称( TestFile.CSV

下载文件
   this.uploadDownloadService.downloadFile(key).subscribe((data) => {
    window.location.href = data;});

我正在设置window.location.href以便下载文件

将fileName覆盖为其他内容然后让用户下载文件的最佳方法是什么?

感谢任何帮助/建议

ZIer0921 回答:Angular-Amazon S3-在下载时从AWS签名的URL更改文件名

我知道您正在使用JS,但是使用Java s3库,您可以在调用预签名的url时将文件设置为具有自定义标头(下载时的文件名,文件类型)。

,

我刚刚发现我可以使用下面的“ ResponseContentDisposition ”解决方案是可以工作的(修改后的代码),如果它可以帮助其他人... https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetObject.html#API_GetObject_RequestSyntax

this.uploadDownloadService.downloadFile(key).subscribe((data) => {
    window.location.href = data;});**strong text**

这会将文件重命名为 TestFile_UserName.CSV

然后将签名的URL设置为href

/* inside a function */
function someAsyncFunction() {

  /* fetching data happening here */

  const data = await res.json();

  const books = [];

  for (let i = 0; i < data.items.length; i++) {
    let titles = data.items[i].title;
    let authors = data.items[i].author;

    if (typeof titles === 'undefined' || typeof authors === 'undefined') {

      // I would like to replace 'undefined' with a string of 'N/A'

    }

    books.push(`${data.items[i].title} by ${data.items[i].authors}`);
  }

  return books;
}
本文链接:https://www.f2er.com/3135767.html

大家都在问