12

I'm able to upload a file to my firebase storage bucket via nodejs using the firebase-admin but when I go to the firebase UI I cannot open the file. I noticed that uploaded files via firebase UI will have an access token automatically generated but no for files uploaded via nodejs.

I already tried several things like setting metadata with downloadtokens and making the file public after it is uploaded. None has worked.

How can I generate the access token via API call rather than having to go to hi and click generate token for each uploaded file?

1
  • Did you ever solve this?
    – NateQ
    Commented Apr 1, 2020 at 2:36

5 Answers 5

12

Full answer as of July 2020:

It is possible to generate a uuid and use it as a token when uploading a file to Firebase Cloud Storage (aka Google Cloud Storage)

First import this package:

const uuidv4 = require('uuid/v4');

or

const { v4: uuidv4 } = require('uuid');

Then, generate a unique id before uploading the file:

const uuid = uuidv4();

Finally, upload your file:

The most important is to embed

metadata: { firebaseStorageDownloadTokens: uuid, }

in the metadata field of upload function

await admin.storage().bucket().upload(filePath, {
                            destination: thumbFilePathForUpload,
                            metadata: {
                                metadata: {
                                    firebaseStorageDownloadTokens: uuid,
                                }
                            },
                        });

In order to check if it worked, click on the newly uploaded file directly from Firebase Console, you should have a blue, clickable link along your file. You can also find the access token under File Location, right under the preview.

For instance:

enter image description here

1
  • 3
    I used this, and it works. I just had to change import line to const { v4: uuidv4 } = require('uuid');.
    – RobiZzT
    Commented Aug 21, 2020 at 8:49
8

I use the below command and it is working perfectly

    const uuidv4 = require('uuid/v4');
    const uuid = uuidv4();
    metadata: { firebaseStorageDownloadTokens: uuid }
0
8

To clarify @Rawan-25's answer further, what you want is:

bucket.upload(filename, {
  destination,
  metadata: {
      metadata :{
        firebaseStorageDownloadTokens: uuidv4(),
     }
  },
})

This is per this Github issue.

2

It's currently not possible to generate an access token with the Firebase Admin SDK. You'll have to do it using one of the client SDKs with the getDownloadUrl method on the StorageReference object. The token is only really intended for use with Firebase client apps.

However, the fact that you can't load a preview in the Firebase console for files uploaded with the Admin SDK is a known issue, and not the way that the console was intended to work. The Firebase team knows about this, but you should still file a bug report anyway with Firebase support to know them know you are impacted by the issue.

1
  • 2
    Hey Doug , is this still an issue in 2023? I working on building a flow where the client only gets the signedUrl from our backend, which works fine with the Firebase Admin SDK as a wrapper, the upload works fine but as you mention there is no preview nor a download URL there. I can get the download URL from the file metadata but it doesn't have an access token, that if I do generate the token manually and construct a FB URL the download works fine. Is there a way to create the access token from the Admin SDK that I am not finding?
    – MorenoMdz
    Commented Jul 4, 2023 at 16:22
2

after an extensive search I managed to get the answer through a reddit post that referred to another stack overflow post lol.

Please take a look at answer #2! Get Download URL from file uploaded with Cloud Functions for Firebase

Not the answer you're looking for? Browse other questions tagged or ask your own question.