Use google cloud function to log file downloads and download file to client

John

I have some publicly available files in a google cloud storage bucket, the links are on a static site. I'd like to log when a user downloads the files, whilst making the link behave like a normal download link.

My initial idea is to use the link to call a cloud function and pass it the object name. The cloud function can do whatever logging logic it needs to, and also pass the file to the client.

My question is about how to pass the file to the client as if they'd just clicked on a normal download link. I think I need to insert some headers, maybe?

Dharmaraj

You can use HTTP functions to log the event and then redirecting user to the actual Google Cloud Storage link. For this would have to use the Cloud function's URL as the user facing URL along with a file identifier param.

Here's a sample function:

exports.downloadFile = (req, res) => {
  const {fileId} = req.query
  // TODO: Get the file URL
  // TODO: Log the event
  res.redirect(FILE_URL);
};

Your function's URL would look something like this:

https://[FN_REGION]-[PROJECT_ID].cloudfunctions.net/[FN_NAME]?fileId=the_file_id

Here's the procedure:

  • When users click on download button, you must open this link in a new tab which will make a GET request to your Cloud function.

  • The function will then read fileId from query param, log the event and redirect user to the actual file URL in their browser.

This still requires the user to click the button once but gives them the actual download URL after the event is logged.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Google Chrome file downloads : Download file size not displayed

File Download not Saving into Downloads Folder

HttpRequest file download downloads a file without extension

How to use scala sttp client to download file

Google Cloud Storage: Download partial audio file

download file from google cloud compute instance

Google Cloud - Download large file from web

download a file from google cloud storage with the api

Firebase Cloud Function Serving Local File for Download

How to Download a file from my Google Drive to my local Downloads file

Laravel 5 file downloads: stream() or download()

How can I use a Google Cloud Function to push a file from a Cloud Storage bucket into an instance?

How to download/store image/file inside Google Cloud Storage bucket by just sending image url using npm GCS client?

Openvpn client log file

Google Cloud Function - Error: ENOENT: no such file or directory

How to load a file from google cloud storage to google cloud function

How to download a file from Google Cloud Platform storage

Download multiple file from Google cloud storage using Python

How to set the destination for file.download() in Google Cloud Storage?

Read/download part of file from google cloud storage in java

How to download a file from the internet to a Google Cloud bucket directly

How to download a file from Google Cloud Storage and return it in Spring controller

Google Cloud Storage download file using only React

Use Cloud Function download a JSON file from URL then upload to a Cloud Storage bucket, status 200 but JSON file uploaded is only 20 bytes and empty

Is there a way to use a google cloud storage bucket file as a local file?

How to open an XML file with python client for google cloud storage

Jersey client to download and save file

How to open a file from google cloud storage into a cloud function

How to setup logging file for cloud function in google cloud console?