我有以下用例。我们正在使用 SSE-C 签名的 AWS 预签名 url。我们需要传递所需的标头以使用和从预签名的 url 下载。 我有下面提到的有效代码。问题是对于大文件,它会出错,并且没有视觉提示客户下载是否正在进行。 有没有一种简单的方法来下载 url 文件(需要标题)。如果标题不在那里,我可以简单地完成 window.open 来启动浏览器下载。 请建议。
function downloadFile() {
const presignedUrl = 'myurl';
//header
const headers = {
'x-amz-server-side-encryption-customer-algorithm': 'AES256',
'x-amz-server-side-encryption-customer-key': 'abc',
'x-amz-server-side-encryption-customer-key-MD5': 'xyz'
};
// fech with header
fetch(presignedUrl, { headers })
.then(response => {
if (response.ok) {
return response.blob();
} else {
throw new Error('Failed to download file');
}
})
.then(blob => {
// Save the file to local machine
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'file_download';
document.body.appendChild(a);
a.click();
a.remove();
})
.catch(error => {
console.error(error);
});
}