I wish to upload files from the browser with metadata that will allow the files to be identified and handled correctly with cloud functions.
On the client my uploader code looks like this:
uploadTaskPromise: async function (file) {
return new Promise((resolve, reject) => {
const storageRef = storage.ref();
const myRef = storageRef.child(`myfolder/${my_file_name}.wav`);
const metadata = {
project_id: my_project_id
};
const uploadTask = myRef.put(file, { metadata: metadata });
uploadTask.on("state_changed",(snapshot) => {
const progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
console.log("Upload is " + progress + "% done");
},
function error(err) {
console.log("error", err);
reject();
},
function complete() {
uploadTask.snapshot.ref.getDownloadURL()
.then(function (downloadURL) {
resolve(downloadURL);
});
}
)
});
},
And on the server (Firebase Cloud Functions using Node js)
exports.uploadHandler = functions.storage.object().onFinalize(async (object) => {
const bucket = admin.storage().bucket(object.bucket);
const metadata = await bucket.file(object.name).getMetadata()
console.log("METADATA: " + JSON.stringify(metadata))
// THIS IS WHERE I WANT TO HANDLE UPLOADS BASED ON THE METADATA
return ({ message: "file uploaded" })
});
The uploadHandler
is working - but metadata
is undefined
, as is metadata.metadata
How can I access the metadata that I uploaded from the client?