Skip to content

FR: Storage for node.js #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
MatiasConTilde opened this issue May 21, 2017 · 13 comments
Closed

FR: Storage for node.js #18

MatiasConTilde opened this issue May 21, 2017 · 13 comments

Comments

@MatiasConTilde
Copy link

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.

@sphippen
Copy link
Contributor

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!).

@schmidt-sebastian
Copy link
Contributor

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.

@mikamaunula
Copy link

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?

@ricsmania
Copy link

@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.

@IStep95
Copy link

IStep95 commented Jul 4, 2019

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?

@schmidt-sebastian
Copy link
Contributor

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.

@IStep95
Copy link

IStep95 commented Jul 12, 2019

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.

@gabrielnvian
Copy link

Any updates on this?

@schmidt-sebastian
Copy link
Contributor

We have not yet been able to prioritize this.

@theprojectsomething
Copy link

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
Copy link

theprojectsomething commented Jan 5, 2021

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

@remorses
Copy link

remorses commented Jan 14, 2021

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'

@schmidt-sebastian
Copy link
Contributor

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.

@firebase firebase locked and limited conversation to collaborators Aug 14, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests