upload jpg file to Firebase Storage from Raspberry-Pi 3 using Node.js

1,061 views
Skip to first unread message

陳體平

unread,
Jan 20, 2018, 11:51:06 AM1/20/18
to Firebase Google Group
Hello,

I'd like to get your advice to solve below issue. 

Now I am working on a project to take photos with a Raspberry Pi 3(Model B) by its camera, and upload photo to firebase storage. My 1st step is to upload a JPG file from local folder as a test. I did it on my desktop PC and it looks working fine. I could check it on the firebase storage console. Then I deploy the file to Raspberry Pi and run the same node.js file again, see my My code below. It is interesting to find error message as below.

I understand it is necessary to find clues from Google Cloud Platform. According to advice from forum, I add the permission under "Storage Administration". See screen cut below. But it is not working. I got same error message when running again on RPi3.

I am exhausting to trial run it without clues. I appreciate a lot if some one can help solve it. 

A. Node.js file 

   
 const firebase = require('firebase');


   
var config = {
 apiKey
: "my_API-key",
 authDomain
: "my_ProjID.firebaseapp.com",
 databaseURL
: "https://my_ProjID.firebaseio.com",
 projectId
: "my_ProjID",
 storageBucket
: "my_ProjID.appspot.com",
 messagingSenderId
: "12800393xxxx"
   
};
   
    firebase
.initializeApp(config);
 
 
const storage = require('@google-cloud/storage')();
 
var bucket = storage.bucket('my_ProjID.appspot.com');
 
var db = firebase.database();
 
var ref = db.ref("/test");
 
    bucket
.upload("./images/jf.jpg", { destination: "images/1.jpg" }, (err, file) => {
 
if (err) { return console.error(err); }
 let publicUrl
= `https://firebasestorage.googleapis.com/v0/b/${config.projectId}.appspot.com/o/${file.metadata.name}?alt=media`;
 
var data = {"URL": publicUrl };
 
ref.set(data);
 
});



B. Error Message

pi@raspberrypi:~/CloudMonitor $ sudo node N04_uploadPhotoToFB.js
{ ApiError: Anonymous users does not have storage.objects.create access to bucke                                     t test-9caab.appspot.com.
    at Object.parseHttpRespBody (/home/pi/CloudMonitor/node_modules/@google-clou                                     d/common/src/util.js:199:30)
    at Object.handleResp (/home/pi/CloudMonitor/node_modules/@google-cloud/commo                                     n/src/util.js:137:18)
    at /home/pi/CloudMonitor/node_modules/@google-cloud/common/src/util.js:496:1                                     2
    at Request.onResponse [as _callback] (/home/pi/CloudMonitor/node_modules/ret                                     ry-request/index.js:195:7)
    at Request.self.callback (/home/pi/CloudMonitor/node_modules/request/request                                     .js:186:22)
    at Request.emit (events.js:159:13)
    at Request.<anonymous> (/home/pi/CloudMonitor/node_modules/request/request.j                                     s:1163:10)
    at Request.emit (events.js:159:13)
    at IncomingMessage.<anonymous> (/home/pi/CloudMonitor/node_modules/request/r                                     equest.js:1085:12)
    at Object.onceWrapper (events.js:254:19)
  code: 401,
  errors:
   [ { domain: 'global',
       reason: 'required',
       message: 'Anonymous users does not have storage.objects.create access to                                      bucket test-9caab.appspot.com.',
       locationType: 'header',
       location: 'Authorization' } ],
  response: undefined,
  message: 'Anonymous users does not have storage.objects.create access to bucket

pi@raspberrypi:~/CloudMonitor $ firebase login
Already logged in as tedd...@gmail.com


C. Firebase Storage Rule

service firebase.storage {
  match
/b/{bucket}/o {
    match
/{allPaths=**} {
      allow read
, write;
   
}
 
}
}


D. Google Cloud Platform Environment


Kato Richardson

unread,
Jan 22, 2018, 3:34:11 PM1/22/18
to Firebase Google Group
Hello,

Any differences in the package.json files between your local machine and the Pi? That seems like a good starting point.

Note that I don't think the firebase-tools login is really what you want for use on your Pi devices. You do not want to deploy your service account credentials on those or use command line auth. 

Instead, you should be using the client node.js tools and implementing an appropriate authentication strategy for your clients. It's hard to guess what would work best for you here without knowing what the devices are and what capabilities they'll have (i.e. if you can make the user authenticate on the device or if you need a custom solution).

☼, Kato

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-talk+unsubscribe@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/03bb7a14-1372-49d2-bd63-1d4ee5d3f467%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

Kato Richardson | Developer Programs Eng | kato...@google.com | 775-235-8398

Reply all
Reply to author
Forward
0 new messages