0

I am currently using the firebase image resizer extension. When a file is uploaded to a specified path, the image resizer deletes the original and provides 3 resized images. In order to grab the image url I have created a firebase storage trigger to grab the url and update a user's document with said url. I am able to do so by obtaining a signed url, but I am storing the signed url on the user document for an extended period of time; thus, I would prefer to grab the image url as seen on the firebase storage bucket. Any and all suggestions would be greatly appreciated.

my code is as follows:

const { functions, db, admin } = require('../util/firebase')

exports.trgUserImg = functions.storage.object().onFinalize(async (object) => {
  const name = object.name

  try {
    const bucket = admin.storage().bucket()
    const file = bucket.file(name)
    const options = {
      action: 'read',
      expires: '03-17-2025'
    }
    const imageUrl = await file
      .getSignedUrl(options)
      .then((results) => {
        const url = results[0]
        return url
      })
      .catch((error) => {
        console.error(new Error(`imageUrl`, error.message))
      })
  } catch (error) {
    console.error(new Error(`trgUserImg: ${error.message}`))
  }

  return null
})
Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121
Rob Terrell
  • 2,398
  • 1
  • 14
  • 36
  • 1
    AFAIK, the only way to get an URL with the Node.js Admin SDK is with the `getSignedUrl()` method. I don't think there will be any other option. – Renaud Tarnec Mar 23 '21 at 15:22
  • 1
    There is actually another way to get a proper download URL by writing a specific set of metadata. See this answer https://stackoverflow.com/a/43764656/209103 (not the accepted answer, but the second-most upvoted one). – Frank van Puffelen Mar 23 '21 at 15:46

0 Answers0