Skip to content
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

Could not authenticate request #106

Closed
kaykhan opened this issue Dec 15, 2017 · 3 comments
Closed

Could not authenticate request #106

kaykhan opened this issue Dec 15, 2017 · 3 comments
Assignees
Labels
api: storage Issues related to the googleapis/nodejs-storage API. type: question Request for information or clarification. Not an issue.

Comments

@kaykhan
Copy link

kaykhan commented Dec 15, 2017

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');
});

@stephenplusplus
Copy link
Contributor

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/{'

@stephenplusplus stephenplusplus added priority: p2 Moderately-important priority. Fix may not be included in next release. type: question Request for information or clarification. Not an issue. labels Dec 15, 2017
@kaykhan
Copy link
Author

kaykhan commented Dec 15, 2017

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

@stephenplusplus
Copy link
Contributor

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')
});

@ghost ghost removed the priority: p2 Moderately-important priority. Fix may not be included in next release. label Dec 15, 2017
@google-cloud-label-sync google-cloud-label-sync bot added the api: storage Issues related to the googleapis/nodejs-storage API. label Jan 31, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: storage Issues related to the googleapis/nodejs-storage API. type: question Request for information or clarification. Not an issue.
Projects
None yet
Development

No branches or pull requests

2 participants