Nodejs-storage: Could not authenticate request

Created on 15 Dec 2017  路  3Comments  路  Source: googleapis/nodejs-storage

Hi,

I am trying to upload a file to my google cloud storage bucket, but i am receiving the error below.

Error: Could not authenticate request
ENOENT: no such file or directory, open '/var/www/project/server/{'
at wrapError (/var/www/project/server/node_modules/gcs-resumable-upload/index.js:24:10)
at /var/www/project/server/node_modules/gcs-resumable-upload/index.js:274:30
at getToken (/var/www/project/server/node_modules/google-auto-auth/index.js:24:9)
at getAuthClient (/var/www/project/server/node_modules/google-auto-auth/index.js:178:9)
at

Environment details

  • OS: Ubuntu 16.04
  • Node.js version: v8.9.3
  • npm version: 5.5.1
  • google-cloud/storage: "^1.5.1",

Steps to reproduce


let Config = require('../../config/app');
let Storage = require('@google-cloud/storage');

class GoogleCloudStorage {

    constructor() {

        this.bucketName = Config.filesystem.google.bucket;

        this.storage = new Storage({
            projectId: Config.filesystem.google.projectId,
            keyFilename: Config.filesystem.google.service_account_key,
        });
    }

    sendUploadToGCS(req, res, next) {

        if (!req.file) {
            return next();
        }

        console.log(req.file);

        const gcsname = Date.now() + req.file.originalname;
        const file = this.storage.bucket(this.bucketName).file(gcsname);
        console.log(file);

        const stream = file.createWriteStream({
            metadata: {
                contentType: req.file.mimetype,
            },
        });

        stream.on('error', (err) => {
            req.file.cloudStorageError = err;
            next(err);
        });

        stream.on('finish', () => {
            req.file.cloudStorageObject = gcsname;
            file.makePublic().then(() => {
                req.file.cloudStoragePublicUrl = getPublicUrl(gcsname);
                next();
            });
        });

        stream.end(req.file.buffer);
    }

    getPublicUrl(filename) {
        return 'https://storage.googleapis.com/' + this.bucketName + '/' + filename;
    }


}

module.exports = GoogleCloudStorage;

app.use('/upload', m.single('avatar'), storage.sendUploadToGCS.bind(storage), (req, res) => {
    console.log(req.file);
    return res.json('1');
});

storage question

Most helpful comment

keyFilename is a string path file name, or you can use a credentials object with the contents of the key file, e.g.

 new Storage({
  projectId: Config.filesystem.google.projectId,

  keyFilename: '/path/to/keyfile.json',
  // or...
  credentials: require('/path/to/keyfile.json')
});

All 3 comments

It looks like the problem is the path of the key file you're providing as keyFilename:

ENOENT: no such file or directory, open '/var/www/project/server/{'

Yeah you may be right, im unsure now whether the service key should be inputted as a string to the function or as an object, assuming i need to add my service key to the keyFileName? to be able to upload to my bucket

keyFilename is a string path file name, or you can use a credentials object with the contents of the key file, e.g.

 new Storage({
  projectId: Config.filesystem.google.projectId,

  keyFilename: '/path/to/keyfile.json',
  // or...
  credentials: require('/path/to/keyfile.json')
});
Was this page helpful?
0 / 5 - 0 ratings