1

I want to get a downloadURL from a file inside my storage from within the onFinalize trigger. In a best case scenario, I want an URL as short as possible (so preferably not a signed one, but just one like the public one like it can be seen in the Firebase Storage UI). Keep in mind, that I am moving the file first, so I cannot access it directly from the onFinalize parameter.

I currently have the following solution:

await imageRef.move(newPath);
const newFile = defaultBucket.file(newPath);
const url = (await newFile.getSignedUrl({
                 action: 'read',
                 expires: '03-09-2491'
}))[0];

This approach has two flaws:

  • Apparently the signed URL is only valid for 3 days. This may be a known issue
  • The URL is very long and takes much space in my Firestore

I also saw an approach, where the URL is being reproduced from the bucket name and the token, but I did not manage to find the token in the metadata of the file.

Community
  • 1
  • 1
Thomas
  • 2,375
  • 2
  • 17
  • 32
  • You say you want a URL like the public URL in the UI. Is that URL acceptable by itself? If so, https://stackoverflow.com/questions/47072883/get-public-url-from-file-uploaded-with-firebase-admin seems to give you what you need. – Conspicuous Compiler May 07 '19 at 00:25
  • That answer is not dealing with the token which is required to get the URL. The first approach is not working, and the second approach is the one I stated here, where the URL is only valid for some days. – Thomas May 07 '19 at 09:16
  • This [answer](https://stackoverflow.com/questions/42956250/get-download-url-from-file-uploaded-with-cloud-functions-for-firebase/43764656#43764656) describes how to get and use the download token. Maybe it's the one you already saw? – alextru May 07 '19 at 11:54
  • Thanks, I tried that. The first problem is, that I don't know how I can access the metadata, since I am copying the file, so I can't pass parameters to the upload process. Also, the `firebaseStorageDownloadTokens` is not documented in any products, so it may be removed in an upcoming version. – Thomas May 07 '19 at 13:35
  • Why do you call 'move()' and not 'upload()'? You could specify the token [like this](https://stackoverflow.com/a/43764656). Note that token metadata is nested. And, just to check, have you tried following the steps in [the docs](https://firebase.google.com/docs/storage/web/download-files)? – alextru May 08 '19 at 08:19

0 Answers0