9

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?

Doug Stevenson
  • 268,359
  • 30
  • 341
  • 380
Rofls
  • 119
  • 1
  • 6

5 Answers5

8

I use the below command and it is working perfectly

    const uuidv4 = require('uuid/v4');
    const uuid = uuidv4();
    metadata: { firebaseStorageDownloadTokens: uuid }
Rawan-25
  • 1,625
  • 1
  • 16
  • 23
8

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

Corentin Houdayer
  • 888
  • 2
  • 8
  • 18
  • 3
    I used this, and it works. I just had to change import line to `const { v4: uuidv4 } = require('uuid');`. – RobiZzT Aug 21 '20 at 08:49
6

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.

NateQ
  • 701
  • 8
  • 13
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.

Doug Stevenson
  • 268,359
  • 30
  • 341
  • 380
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

jpaz
  • 21
  • 2