Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

398
Views
How to delete a Cloud Storage file after it has been downloaded?

I have a Cloud Function that deletes the file from Cloud Storage

Function:

const { Storage } = require("@google-cloud/storage");
const storage = new Storage();

let handler = async (file, context) => {
    console.log(`  Event: ${context.eventId}`);
    console.log(`  Event Type: ${context.eventType}`);
    console.log(`  Bucket: ${file.bucket}`);
    console.log(`  File: ${file.name}`);

    let bucket = storage.bucket(file.bucket);
    let bucketFile = bucket.file(file.name);

    bucketFile.delete();
};

I want to trigger this function after any file in the bucket is downloaded.

I've looked at the Cloud Storage triggers,

google.storage.object.finalize (default)

google.storage.object.delete

google.storage.object.archive

google.storage.object.metadataUpdate

but they don't work the way I want them to. Anyone have any suggestions?

Edit:

If I need to explain a little more what I want to do; Whether registered in our system or not, users are given links with some of their data openly to the public. Since these links may contain sensitive data, I would like to be granted a one-time download right. After a single download, the data needs to be permanently deleted.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can catch the get event if you activate the audit logs on Cloud Storage (be careful, the audit logs on Cloud Storage can generate a lot of logs volume and can cost).

When the audit logs are activated, you can filter on that method name:

methodName: "storage.objects.get"

Of course you can add other filters on Cloud Logging, like the resourceName to filter on the bucket and/or the file prefix/suffix (use the =~ for the regex expression)

When your filter is OK and you get only the entries that you expect, create a sink to PubSub, then a Push PubSub subscription to invoke your piece of code (on Cloud Functions or on Cloud Run).

You will receive the Cloud Logging JSON entry, get the resourceName in the JSON to know which file has been downloaded, and then delete it.

about 4 years ago · Juan Pablo Isaza Report

0

Another solution is to use Object lifecycle, you can automatically delete an object after it creation for 1 day (days are the minimum unit)

You can find the Lifecycle setting in the bucket details page:

enter image description here

And then you have to add a rule like that for deleting objects after 1 day:

enter image description here

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!