Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 669
Rofls Avatar asked Dec 21 '19 00:12

Rofls


People also ask

How do I get Firebase Storage access token?

The download token is created automatically whenever a file is uploaded to Cloud Storage for Firebase. It's a random UUIDv4, which makes the URL hard to guess. To retrieve a download URL, your client app needs to call the getDownloadUrl() method on the file reference.

How can I get URL of uploaded image in Firebase Storage?

Image URL is obtained by uploading an image to firebase bucket and then that can return back a URL that URL is a permanent URL which can be open anywhere. Then a user can use this URL for any purpose in its application.

How do I get files from Firebase Storage?

To download a file, first create a Cloud Storage reference to the file you want to download. You can create a reference by appending child paths to the root of your Cloud Storage bucket, or you can create a reference from an existing gs:// or https:// URL referencing an object in Cloud Storage.


3 Answers

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

like image 86
Corentin Houdayer Avatar answered Oct 21 '22 07:10

Corentin Houdayer


I use the below command and it is working perfectly

    const uuidv4 = require('uuid/v4');
    const uuid = uuidv4();
    metadata: { firebaseStorageDownloadTokens: uuid }
like image 8
Rawan-25 Avatar answered Oct 21 '22 07:10

Rawan-25


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.

like image 8
NateQ Avatar answered Oct 21 '22 08:10

NateQ