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?
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.
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.
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.
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:
I use the below command and it is working perfectly
const uuidv4 = require('uuid/v4');
const uuid = uuidv4();
metadata: { firebaseStorageDownloadTokens: uuid }
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With