How can I generate access token to file uploaded to firebase storage?
Asked Answered
E

5

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?

Embolectomy answered 21/12, 2019 at 0:23 Comment(1)
Did you ever solve this?Sarsenet
F
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

Flam answered 28/7, 2020 at 16:46 Comment(1)
I used this, and it works. I just had to change import line to const { v4: uuidv4 } = require('uuid');.Nesline
P
8

I use the below command and it is working perfectly

    const uuidv4 = require('uuid/v4');
    const uuid = uuidv4();
    metadata: { firebaseStorageDownloadTokens: uuid }
Prowl answered 2/1, 2020 at 6:19 Comment(0)
S
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.

Sarsenet answered 1/4, 2020 at 2:44 Comment(0)
F
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.

Feder answered 21/12, 2019 at 0:27 Comment(1)
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?Titanism
L
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

Lynlyncean answered 28/12, 2019 at 4:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.