Firebase Storage / Bucket public download using 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 was successfully uploaded to my firebase repository, but:

enter image description here The preview image on the right side is not displayed, and I cannot upload the image using the upload button. In addition, there is no boot url (which I do not mind, because I can access it from the link above) and when you click "Create a new download URL" Firebase gives

Error creating upload URL

When deleting public: true preview is displayed correctly, and I can also create the download url. But the public url https://storage.googleapis.com/project-id.appspot.com/uploads/12345/full.jpg will no longer work, which is necessary for me.

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

+5
source share
2 answers

1) It does not expire while it is always open. (I added a link related to this below)

2) Here is a quick fix. (You can do one of the following)

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

gsutil defacl ch -u your_email@mail.com : OWNER gs: // your_bucket;

  • Add yourself as a repository administrator in the Google IAM admin console

Then reload the file ...

TL DR

Here's why, according to the Firebase Documentation :

You can use the Google Cloud Storage API to access files downloaded via the Firebase SDK for cloud storage, especially to perform more complex operations, such as copying or moving a file, or listing all the files available by reference.

It’s important to note that these requests use Google Cloud ACL or Project IAM cloud storage, not Firebase Authentication and Storage Security Policies.

Regarding public:true , I think it will enable and create a public link. Contact here

+4
source

Your URLs should not change or end, and they must be valid until they are canceled, which can happen by updating the object or explicitly by invalidating the URLs in the firebase web console. At least as far as I know.

The first thing that comes to my mind is that you configured CORS on a bucket? This may be one of the reasons. check this

The second thing I can think of is storage rules. If you have something like allow read: if request.auth.uid == userId; , you can delete them to make the files public. You can use something like this to make all files in the path public:

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

If you have already done these two things, then, unfortunately, this may be a mistake in firebase. Good luck

+2
source

Source: https://habr.com/ru/post/1268383/


All Articles