I have created a Cloud Function that trigger on any new file upload in Firebase Storage. Once successful upload function will update its metadata, but even though setting new metadata with 'setMetadata()' is not getting applied. There is no error during the process and but on checking for updated metadata, the new one is not reflecting.
exports.onImageUpload = functions.storage.object().onFinalize(async (object) => {
const storageRef = admin.storage().bucket(object.bucket);
var metadata = {
'uploader': 'unknown'
}
await storageRef.file(object.name).setMetadata(metadata).then(function(data) {
console.log('Success');
console.log(data);
return;
}).catch(function(error) {
console.log(error);
return ;
});
return;
});
There is no error, and on Cloud Function log its printing 'Success' message. Also "metageneration: '2'" property also got updated, which means it should have updated metadata with new values, but it didn't.
File metadata contains common properties such as name , size , and contentType (often referred to as MIME type) in addition to some less common ones like contentDisposition and timeCreated . This metadata can be retrieved from a Cloud Storage reference using the getMetadata() method.
What is file metadata? Metadata provides information about something, usually in greater or more organized detail. File metadata is when this metadata is about a digital file type or is stored in a dedicated file for future use.
There are no move or rename operations provided by the Firebase SDKs. You would have to write code to download the file, upload it again with the new name, then delete the original. It's better if you don't rely on the name of files in storage.
The problem comes from the fact that if you want to set custom key/value pairs they must be in the metadata
key of the object you pass to the setMetadata()
method, i.e. the metadata
object in your case. This is explained in the API Reference Documentation for node.js.
So the following will work:
exports.onImageUpload = functions.storage.object().onFinalize(async (object) => {
const storageRef = admin.storage().bucket(object.bucket);
var metadata = {
metadata: {
'uploader': 'unknown'
}
}
try {
const setFileMetadataResponse = await storageRef.file(object.name).setMetadata(metadata);
console.log('Success');
console.log(setFileMetadataResponse[0]);
return null;
} catch (error) {
console.log(error);
return null;
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With