Reputation: 2466
Is it possible to upload files using cloud functions? This is my attempt:
const firebase = require("firebase");
app.post('/images', async function (request, response) {
const data = request.body;
var file = data.image; // base64 image data
const storageRef = firebase.storage().ref(`images/${data.name}`);
storageRef.putString(file, 'data_url').then(function (snapshot) {
response.send('success')
});
})
and I'm getting error UnhandledPromiseRejectionWarning: TypeError: firebase.storage is not a function
is there any example that I can find?
Upvotes: 1
Views: 1182
Reputation: 26313
Yes, it's possible. In a Cloud Function, generally you should be using the Admin SDK (firebase-admin
not firebase
). Note that the Admin SDK bypasses security rules, so you should make sure to handle authorization for uploading properly.
In a broader sense, however, I don't think this is actually what you probably want to do. Cloud Functions have relatively low limits on request size (10MB) and you have to pay for the compute time during the upload. Instead, why not just use the JS SDK in the application itself? Your users can still upload directly into the Cloud Storage bucket, but without incurring the extra cost of running through a function first.
Upvotes: 2