2
votes

How to write a Firebase http function that returns an url (in plain text) where an image can be downloaded?

I am using the latest version of Firebase Admin SDK. I have a file called images/thumbnail.png in the default bucket for which I would like to return the URL via a HTTP call, I came up with this code:

exports.getImageUrl = functions.https.onRequest((request, response) => {
  return firebase.storage().bucket().file("images/thumbnail.png").getSignedUrl({
    action: 'read',
    expires: '03-09-2491'
  })
  .then(signedUrls => {
    return response.send(signedUrls[0])
  })
  .catch(error => {
    return response.status(500).send(error)
  })
})

but this fails with :

callback is not a function
    at /user_code/node_modules/firebase-admin/node_modules/@google-cloud/storage/src/file.js:1784:7
    at Request._callback (/user_code/node_modules/firebase-admin/node_modules/google-auto-auth/index.js:362:9)

What am I doing wrong?

EDIT

I added logging in detail like this:

exports.getImage = functions.https.onRequest((request, response) => {
  console.log("start")
  const storage = firebase.storage()
  console.log(storage)
  const bucket = storage.bucket()
  console.log(bucket)
  const file = bucket.file("buildInExercises/bb_4zigzagcushionblue_thumbnail.png")
  console.log(file)
  return file.getSignedUrl({
    action: 'read',
    expires: '03-09-2491'    
  })
  .then(urls => {
    return response.send(urls)
  })
  .catch(error => {
    return response.status(500).send(error)
  })
})

I am now getting a proper reply:

{
  "message": "Permission iam.serviceAccounts.signBlob is required to perform this operation on service account projects/snookercoachapp-dev/serviceAccounts/snookercoachapp-dev@appspot.gserviceaccount.com."
}

It seams I have a permission issue?

1
Try updating to the latest version of the admin SDK. The error is coming from inside the cloud storage SDK. - Doug Stevenson
Also bear in mind that getSignedUrl currently only works if you init the admin SDK with service account credentials. - Doug Stevenson
my other cloud functions work fine and I am running the latest version of the admin SDK. I think I am use the function the wrong way or something? - HixField
I dunno, try logging all over to the place to see what's happening. - Doug Stevenson
in such a simple function I cannot put much logging in... - HixField

1 Answers

2
votes

The error message is a symptom of the face that getSignedUrl() only works when you initialize the admin SDK with a service account. This means you can't depend on the default credentials that are used when you call admin.initializeApp() with no arguments.