Renaming a file – Google API direct download link

I am using an external API to create and download a data extract. After the extract has been created, the API response includes a url. This url is a direct download link – I click it and a new browser tab opens, which automatically downloads the file to my downloads folder. I am trying to automate this process so I don’t have to manually download and rename the file. Everything is working, except the file is always downloaded with a default name. We’ll say this is “123456123456.csv.gz” to keep it ambiguous.

The link I follow is formatted like this-

https://storage.googleapis.com/(custom storage name)/(data extract id)/123456123456.csv.gz?X-Goog-Algorithm=GOOG4-RSA-SHA256&X-Goog-Credential=(google service account credentials)&X-Goog-Date=(date)&X-Goog-Expires=(time until expiration)&X-Goog-SignedHeaders=host&X-Goog-Signature=(long string of number and letters)

I am currently using puppeteer to just open a new browser tab to that url, which downloads the file, and then close puppeteer-

async function downloadFile (url) {
  try {
    const browser = await puppeteer.launch(
      {headless: false}
    );
    const page = await browser.newPage();
    await page.goto(`${url}`);
    await page.waitForTimeout(3000); // just in case it takes a second
    await browser.close();
  }
  catch (error) {
    console.error(error);
  }
}

I tried to directly edit the link with a custom file name like this-

...
  const autoFileName = '000000000000.csv.gz';
  const customFileName = `${date}-${reportingLevel}.csv.gz`;
  for (let i = 0; i < urls.length; i++) {
    const newUrl = url.replace(autoFileName, customFileName);
    downloadFile(newUrl);
...

But when I open the new url in a browser, instead of downloading a file it gives me a page like this-

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<Error>
<Code>SignatureDoesNotMatch</Code>
<Message>Access denied.</Message>
<Details>The request signature we calculated does not match the signature you provided. Check your Google secret key and signing method.</Details>
<StringToSign>(sensitive information)</StringToSign>
<CanonicalRequest>GET (sensitive information) UNSIGNED-PAYLOAD</CanonicalRequest>
</Error>

I can’t find any documentation from google about these links – if anyone can find any I would love it!

At first I thought the ‘123456123456.csv.gz’ section of the url was just the name to download the file as, but now I’m realizing the file is most likely stored in their google storage as ‘123456123456.csv.gz’.

Does anyone have ideas on how I can have this automatically rename my files so I don’t have to do it manually? I did try using axios/fs to download instead, but had no luck even downloading the file, not to mention renaming it. I haven’t used axios much before so it’s possible I was doing it wrong.

Thanks in advance!