Reputation: 2189
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.
Upvotes: 2
Views: 1392
Reputation: 3935
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:
And then you have to add a rule like that for deleting objects after 1 day:
Upvotes: 2
Reputation: 75930
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.
Upvotes: 3