1

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?

1
  • What does your package.json look like? Commented Oct 18, 2018 at 20:15

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.