1
votes

I'm trying to upload an image to Google Cloud Storage using the simple code locally on my machine with my service account:

const storage = require('@google-cloud/storage');
const fs = require('fs');
const gcs = storage({
    projectId: 'ID',
    keyFilename: 'KEYNAME'
});
var bucket = gcs.bucket('BUCKET NAME');

bucket.upload('hiking-image.jpg', function(err, file) {
    if (err) throw new Error(err);
});

if (err) throw new Error(err);
         ^

However, I get the error message below. Is the Google Cloud API only supposed to work when deployed on App Engine or am I doing something wrong here? I was able to get the Google Vision API to work locally using the same Service Account.

Error: ApiError: Forbidden
    at /Users/user/google-cloud/upload/upload.js:10:20
    at Pumpify.<anonymous> (/Users/user/node_modules/@google-cloud/storage/src/bucket.js:1218:9)
    at emitOne (events.js:101:20)
    at Pumpify.emit (events.js:188:7)
    at Pumpify.Duplexify._destroy (/Users/user/node_modules/duplexify/index.js:184:15)
    at /Users/user/node_modules/duplexify/index.js:175:10
    at _combinedTickCallback (internal/process/next_tick.js:67:7)
    at process._tickCallback (internal/process/next_tick.js:98:9)
2
Question is quite old, but did you authorize the service account API access to cloud storage ?Overdrivr
Indeed :) It was simply an error with the service account in the Google Cloud Consoleuser3642173
I've made a more documented answer, feel free to accept it if it suits you. Cheers !Overdrivr

2 Answers

1
votes

You need to give appropriate role to your service account. Go to the google cloud console, menu "IAM and Admin"

  1. Create a service account
  2. Go to IAM panel
  3. Edit the role of the service account you just created. Select cloud storage and make it either a full admin, but the best is you just make it a creater or reader of storage objects depending on your needs.

google cloud console screenshot

0
votes

I simply had an error in the cloud console with my service account at Overdrivr pointed out