firebase-js-sdk icon indicating copy to clipboard operation
firebase-js-sdk copied to clipboard

FR: Storage for node.js

Open MatiasConTilde opened this issue 9 years ago • 13 comments

There is a way to use it with Google Cloud, but it is a bit complicated and firebase storage directly for web is much easier and convenient to use.

MatiasConTilde avatar May 21 '17 18:05 MatiasConTilde

This is definitely on our radar. There's stuff that many developers would want to do in a Node.js environment that doesn't even exist in the current Storage JS API, like file downloads.

As you alluded to, https://github.com/GoogleCloudPlatform/google-cloud-node generally works as a replacement for server-side use cases where you can use service account credentials. The API is different, but it lets you do things the JS API doesn't support right now (file downloads!).

sphippen avatar May 31 '17 00:05 sphippen

The Firebase Admin SDK for Node has recently added support for Storage by wrapping the Google Cloud Storage libraries: https://firebase.google.com/docs/storage/admin/start. This makes it a lot easier to integrate GCS with Firebase (as it supports Firebase Authentication). While this integration does not unify the API surface, it simplifies the initial setup.

schmidt-sebastian avatar Sep 13 '17 20:09 schmidt-sebastian

Firebase js sdk has now support for nodejs #221. Thank you for that!! We have a need for storage api also. Is there any possibility to add nodejs support?

mikamaunula avatar Dec 04 '17 12:12 mikamaunula

@MatiasConTilde could you please provide an example on how to use Google Cloud to access Firebase Storage? I tried it but I'm getting an error:

Anonymous users does not have storage.objects.get access to mybucket.appspot.com/object.txt

My Firebase Storage is public and I'm not authenticating, so I think it should work.

I have never used Google Cloud directly, do I have to create an account there and configure it too?

I tried setting an incorrect bucket and I got a different error, so I guess at least that part is correct.

ricsmania avatar Jan 05 '18 12:01 ricsmania

Why it is not possible to upload with this configuration to any bucket? Like on frontend(Angular2+) application it is possible, why is it different on backend(node.js)? var firebaseConfig = { apiKey: "YOUR-API-KEY", authDomain: "YOUR-AUTH-DOMAIN", databaseURL: "YOUR-DATABASE-URL", projectId: "YOUR-PROJECT-ID", storageBucket: "YOUR-STORAGE-BUCKET", messagingSenderId: "YOUR-MESS-SENDER-ID", appId: "YOUR-APP-ID" };

I know I can accomplish upload with google cloud storage api but than I need public and private key generated from google cloud platform. I am very interested will it be implemented soon, or it will not?

IStep95 avatar Jul 04 '19 15:07 IStep95

We are investigating what it would take to ship a Firebase Storage SDK for Node (with third-party auth), but we cannot promise when or if this will be available soon.

schmidt-sebastian avatar Jul 09 '19 16:07 schmidt-sebastian

Thank you for the fast answer, but the point is that I can upload to storage from frontend(Angular 2+) application only with configuration I mentioned the same thing happens with firestore or RealTime database it is possible to upload from frontend. I am very interested why it is not possible from backend side. With node.js or .NET(C#). It requires me before I want to upload to storage or firestore to be authenticated.

IStep95 avatar Jul 12 '19 18:07 IStep95

Any updates on this?

gabrielnvian avatar Sep 25 '20 17:09 gabrielnvian

We have not yet been able to prioritize this.

schmidt-sebastian avatar Sep 29 '20 17:09 schmidt-sebastian

Old issue but a goodie .. currently running into issues around this while working with iot (insecure) and 3rd party credentials. Workaround is exceptionally obtuse. A standard, rules-respecting and metadata applying API, even with major caveats, would be fantastic!

theprojectsomething avatar Jan 05 '21 01:01 theprojectsomething

Answering my own comment .. patching the storage library with an XHR polyfill actually appears to work (Node 15 / minimal testing so far). Specifically I've installed xmlhttprequest-ssl and imported it at the top of the storage module (in my case line 5 in @firebase/storage/dist/index.cjs.js):

var XMLHttpRequest = require("xmlhttprequest-ssl").XMLHttpRequest;

Back in the app I import firebase/storage as normal, then just load files from the local file system and upload as per the standard Firebase API:

import { firebase } from "@firebase/app";
import "@firebase/storage";

/* include Firebase setup, etc. */

const filename = 'offline-user-generated-image.jpg';
const file = fs.readFileSync(filename); // for brevity only
const ref = firebase.storage().ref().child(`test/${filename}`);

// use the buffer's underlying arraybuffer 
ref.put(file.buffer, {
  contentType: 'image/jpg', // defaults to 'application/octet-stream'
  customMetadata: {
    uid: 'abc123', // for enhanced security rules
  },
})
.then(() => console.log('done'))
.catch(e => console.log(e));

Storage rules apply to uploads (woop!) and security tokens and custom metadata are applied as expected. Also tested with `putString'. This solves some fairly drastic workarounds for my use case, as it means I can keep my logic/auth/etc. entirely within the Firebase ecosystem. Keen to hear others thoughts.

Note: I'm using patch-package to ensure the patch stays intact across installs / upgrades.

Update: I had to further patch the storage module (to clear an unresolved timeout) to allow the node process to complete successfully ... See PR #4266

theprojectsomething avatar Jan 05 '21 03:01 theprojectsomething

My use case is uploading files from a cli without needing to setup a server

I think that it shouldn't require too much effort, just polyfill XMLHttpRequest and everything should work

Currently i am doing this, upload works but node never exits

global.XMLHttpRequest = require('xmlhttprequest-ssl').XMLHttpRequest
import { firebase } from '@firebase/app'
import '@firebase/storage'
import '@firebase/auth'

remorses avatar Jan 14 '21 16:01 remorses

This will be available with the next release of Firebase. You should be able to run the Storage SDK in Node with no additional setup.

The Node implementation does not support fine-grained upload progress indications, as upload progress is only available via XMLHTTPRequest. You will still receive some upload progress events for large files as we use chunked uploads.

schmidt-sebastian avatar Jul 14 '21 21:07 schmidt-sebastian