user3642173
user3642173

Reputation: 1255

Uploading file to Google Cloud storage locally using NodeJS

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)

Upvotes: 1

Views: 1129

Answers (2)

Overdrivr
Overdrivr

Reputation: 6576

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

Upvotes: 1

user3642173
user3642173

Reputation: 1255

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

Upvotes: 0

Related Questions