Chris
Chris

Reputation: 4370

Firebase Storage/Bucket public upload with node.js

Code:

var storage = require('@google-cloud/storage');
var gcs = storage({
    projectId: config.google.projectId,
    keyFilename: config.google.keyFilenameFirebase
});

var bucket = gcs.bucket('project-id.appspot.com');
var destination = 'uploads/12345/full.jpg';

bucket.upload(myFile, { public: true, destination: destination }, function(err, file) {
    if (err) {
        console.log(err);
    }
});

My file is successfully uploaded to my firebase storage, but:

  1. The file is now publicly accessible through the url: https://storage.googleapis.com/project-id.appspot.com/uploads/12345/full.jpg. Can I use this fixed URL in my App or is it likely, that it will change or expire?
  2. public: true seems to break the Firebase Storage UI:

enter image description here The preview of the image on the right side is not showing and I'm unable to download the image via the download button. Also, there is no download url (which I don't mind, cause I can access it via the link above) and when clicking "Create new download URL" Firebase yields

Error generating download URL

When removing public: true the preview is shown correctly and I can also generate a download URL. But the public url https://storage.googleapis.com/project-id.appspot.com/uploads/12345/full.jpg won't work anymore, which is necessary for me.

So my plan is to stick to public: true, but it still bugs me, that the preview/download button is not working and it looks like a bug in Firebase to me. Can anyone confirm that?

Upvotes: 9

Views: 9839

Answers (2)

goblin
goblin

Reputation: 1553

1) It will not expire as long as it is always public. (I added a link related to this at the bottom)

2) Here's the quick fix. (You can do either of the following)

  • Add yourself in the default ACL (below is the command using gsutil)

gsutil defacl ch -u [email protected]:OWNER gs://your_bucket;

  • Add yourself as a Storage Admin in the Google IAM admin console

Then reupload your file....

TL;DR

Here's the reason why, according to the Firebase Documentation:

You can use the Google Cloud Storage APIs to access files uploaded via Firebase SDKs for Cloud Storage, especially to perform more complex operations, such as copying or moving a file, or listing all the files available at a reference.

It's important to note that these requests use Google Cloud Storage ACLs or Project Level IAM, rather than Firebase Authentication and Storage Security Rules.

With regard to the public:true, I think that it will only enable and create the public link. Refer here

Upvotes: 8

johnozbay
johnozbay

Reputation: 2222

Your URLs shouldn't change or expire, and they should be valid until revoked which can happen through an object update or explicitly through revoking a url in the firebase web console. At least to the best of my knowledge.

First thing that comes to my mind is that : have you configured CORS on the bucket? This could be one reason. Check this out

Second thing that I can think of is storage rules. If you have something like allow read: if request.auth.uid == userId; you might want to remove those to make files publicly accessible. You can use something like this to make all files in a path public:

 service firebase.storage {
  match /b/XXXXXX.appspot.com/o {
    match /{allPaths=**} {
      allow read;
    }
  }

If you've already done these two things, then unfortunately it might actually be a firebase bug too. Best of luck!

Upvotes: 3

Related Questions